Add naive solution to part 1
This didn't run for my actual input, but it works on the small one
This commit is contained in:
parent
754d998b24
commit
2600012663
1 changed files with 41 additions and 0 deletions
41
python/2023/src/andy_aoc_2023/day5.py
Normal file
41
python/2023/src/andy_aoc_2023/day5.py
Normal file
|
@ -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)}")
|
Loading…
Reference in a new issue