From 2eef455f9d63810cc1bb4042b76b1506d666ff83 Mon Sep 17 00:00:00 2001 From: Jake Charman Date: Sun, 10 Dec 2023 20:39:04 +0000 Subject: [PATCH] 2023 Day 5 (P1 only) --- 2023/Day-05/day05-1.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 2023/Day-05/day05-1.py diff --git a/2023/Day-05/day05-1.py b/2023/Day-05/day05-1.py new file mode 100755 index 0000000..37321ef --- /dev/null +++ b/2023/Day-05/day05-1.py @@ -0,0 +1,44 @@ +#!/usr/bin/python3 + +def main(): + seeds = [] + ranges = {} + + with open('input.txt', 'r') as input_file: + line = input_file.readline().strip() + while line != '': + if line.startswith('seeds:'): + seeds = [int(seed) for seed in line.split(':')[1].split()] + input_file.readline() + line = input_file.readline().strip() + else: + map = line.split()[0] + ranges[map] = {} + line = input_file.readline().strip() + while line != '': + digits = [int(num) for num in line.split()] + dest_range = range(digits[0], digits[0] + digits[2]) + source_range = range(digits[1], digits[1] + digits[2]) + ranges[map][source_range] = dest_range + line = input_file.readline().strip() + line = input_file.readline().strip() + + locations = [] + + for seed in seeds: + next_val = seed + for map in ranges: + for source in ranges[map]: + if next_val in source: + difference = source.start - ranges[map][source].start + next_val = next_val - difference + break + else: + next_val = next_val + + locations.append(next_val) + + print(min(locations)) + +if __name__ == '__main__': + main() \ No newline at end of file