Add day 5 solutions

This commit is contained in:
Andy Lu 2023-12-07 20:59:47 -05:00
parent 2600012663
commit 472ff4e1ab
4 changed files with 108 additions and 16 deletions

View file

@ -44,3 +44,24 @@ where =avg-time= is [[https://stackoverflow.com/a/54920339][this bit of ~awk~]]
*** Answers
19135
5704953
** Day 5
*** Answers
324724204
104070862
#+begin_src shell
-> date -Iseconds; cat src/andy_aoc_2023/BIG_INPUTS | xargs -P 0 -I '%' python src/andy_aoc_2023/day5.py src/andy_aoc_2023/BIG_MAPS %; date -Iseconds
2023-12-05T12:55:55-05:00
856496588
547766656
990928320
104899384
516299014
104070862 <-- This is the min aka the answer
324724204
344671053
114257961
146071405
2023-12-05T14:46:41-05:00
#+end_src

View file

@ -0,0 +1,10 @@
4106085912 135215567
529248892 159537194
1281459911 114322341
1857095529 814584370
2999858074 50388481
3362084117 37744902
3471634344 240133599
3737494864 346615684
1585884643 142273098
917169654 286257440

File diff suppressed because one or more lines are too long

View file

@ -6,16 +6,15 @@ import sys
def parse_raw_input(lines):
chunks = []
input_map = {}
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)))
input_map.append((dest, source, range_length))
else:
if input_map:
chunks.append(input_map)
input_map = {}
input_map = []
# Need to get the last lines processed
if input_map:
@ -23,19 +22,80 @@ def parse_raw_input(lines):
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):
def part1(seeds, input_maps, states):
for seed in seeds:
state = seed
for input_map in input_maps:
new_state = input_map.get(state, state)
new_state = state
for partition in input_map:
dest, source, range_length = partition
if source <= state < source + range_length:
offset = dest - source
new_state = state + offset
break
state = new_state
states.append(state)
print(f"{min(states)}")
return min(states)
# print(f"{min(states)}")
# 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())
# part1(map(int, seeds), input_maps, [])
def partition(input, size):
for i in range(0, len(input), size):
yield input[i: i + size]
# states = []
# for start, size in partition(list(int(s) for s in seeds), 2):
# print(start)
# seed_range = range(start, start + size)
# part1(seed_range, input_maps, states)
# # part1((start, start + size), input_maps, states)
# print(f"{states} : {min(states)}")
# print(f"{min(states)}")
# for start, size in partition(list(int(s) for s in seeds), 2):
# print(start, size)
import json
# print(json.dumps(input_maps))
with open(sys.argv[1]) as infile:
input_maps = json.load(infile)
start, length = map(int, sys.argv[2].split(" "))
# length = sys.argv[3]
# print(f"Would run with maps {len(input_maps)} {start} {length}")
seed_range = range(start, start + length)
print(part1(seed_range, input_maps, []))
#print(input_maps)
# for seed in map(int, seeds):
# state = seed
# for input_map in input_maps:
# new_state = state
# for partition in input_map:
# dest, source, range_length = partition
# if source <= state < source + range_length:
# offset = dest - source
# new_state = state + offset
# break
# state = new_state
# states.append(state)
# print(f"{min(states)}")