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.
66 lines
1.5 KiB
66 lines
1.5 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("things sorted ", list(sorted(consec_maps, key=lambda x: x[-1])))
|
||
|
|
||
|
best = 9999999999999999
|
||
|
for i in range(0,len(seeds),2):
|
||
|
for x in range(seeds[i], seeds[i]+seeds[i+1]):
|
||
|
candidate = consecutive_maps(maps, x)
|
||
|
if candidate[-1] < best:
|
||
|
best = candidate[-1]
|
||
|
|
||
|
print(best)
|