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/day05/aoc.py

54 lines
1.5 KiB

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
# maps are lists of numbers, the output they map to, and the range in question
def transform(this_mapling, that_mapling):
our_origin = {start: this_mapling[1], end: this_mapling[1]+this_mapling[2]}
our_destination = {start: this_mapling[0], end: this_mapling[0] + this_mapling[2]}
their_origin = {start: that_mapling[1], end: that_mapling[1]+this_mapling[2]}
their_destination = {start: that_mapling[0], end: that_mapling[1] + that_mapling[2]}
# our destination start is in their origin range