From 2600012663c1fc201eeb22c9722c68a2d9e7b9ea Mon Sep 17 00:00:00 2001 From: Andy Lu Date: Tue, 5 Dec 2023 11:29:32 -0500 Subject: [PATCH] Add naive solution to part 1 This didn't run for my actual input, but it works on the small one --- python/2023/src/andy_aoc_2023/day5.py | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 python/2023/src/andy_aoc_2023/day5.py diff --git a/python/2023/src/andy_aoc_2023/day5.py b/python/2023/src/andy_aoc_2023/day5.py new file mode 100644 index 0000000..8d7b263 --- /dev/null +++ b/python/2023/src/andy_aoc_2023/day5.py @@ -0,0 +1,41 @@ +from collections import defaultdict +from pprint import pprint +import re +import sys + + +def parse_raw_input(lines): + chunks = [] + input_map = {} + for line in lines: + if (matches := re.match(r"(\d+) (\d+) (\d+)", line)): + dest, source, range_length = map(int, matches.groups()) + input_map = input_map | dict(zip(range(source, source + range_length), + range(dest, dest + range_length))) + else: + if input_map: + chunks.append(input_map) + input_map = {} + + # Need to get the last lines processed + if input_map: + chunks.append(input_map) + + return chunks + +input_file = sys.argv[1] + +with open(input_file) as infile: + seeds = infile.readline().strip().split(": ")[1].split(" ") + input_maps = parse_raw_input(infile.readlines()) + + +states = [] +for seed in map(int, seeds): + state = seed + for input_map in input_maps: + new_state = input_map.get(state, state) + state = new_state + states.append(state) + +print(f"{min(states)}")