advent of code 2023 edition
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aoc_2023/aoc_05.py

57 lines
1.3 KiB

10 months ago
import re
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="the file from which to take data")
args = parser.parse_args()
seeds = []
maps = [] # list of map lists
with open(args.filename, "r") as f:
seeds_re = re.compile("seeds: ((\d| )*)")
new_map_re = re.compile("(.*?) map:")
map_data_re = re.compile("\d+ \d+ \d+")
current_map = []
for l in f.readlines():
if seeds_re.match(l):
seeds = list(map(int, seeds_re.match(l).group(1).split()))
if new_map_re.match(l):
if current_map:
maps.append(current_map)
current_map = []
if map_data_re.match(l):
current_map.append(list(map(int, map_data_re.match(l).group(0).split())))
if current_map: maps.append(current_map)
print("seeds: ", seeds)
print("maps are: ")
for x in maps: print(x)
def mapit(the_map, number):
for destination_start, origin_start, length in the_map:
offset = number - origin_start
if offset < length and offset >= 0:
return destination_start + offset
return number
def consecutive_maps(all_maps, number):
output = []
output.append(number)
for the_map in all_maps:
number = mapit(the_map, number)
output.append(number)
return output
consec_maps = [consecutive_maps(maps, s) for s in seeds]
for s in consec_maps: print(s)
print(list(sorted(consec_maps, key=lambda x: x[-1])))