This commit is contained in:
Sijmen 2023-12-06 01:36:05 +01:00
parent 6fdf2e641d
commit 89f279462e

View file

@ -1,12 +1,6 @@
import fileinput
import sys
input = fileinput.input()
seeds = [int(seed) for seed in next(input).split()[1:]]
next(input)
next(input)
def parse_map(input):
try:
@ -19,15 +13,6 @@ def parse_map(input):
yield (0, 0, sys.maxsize)
seed_to_soil = list(parse_map(input))
soil_to_fert = list(parse_map(input))
fert_to_water = list(parse_map(input))
water_to_light = list(parse_map(input))
light_to_temp = list(parse_map(input))
temp_to_humid = list(parse_map(input))
humid_to_loc = list(parse_map(input))
def transform_ranges(map, ranges):
for start, length in ranges:
# find the start of the range
@ -44,38 +29,34 @@ def transform_ranges(map, ranges):
yield from transform_ranges(map, [(src_end, end - src_end)])
def part1():
result = sys.maxsize
def do_the_thing(ranges):
soils = transform_ranges(seed_to_soil, ranges)
ferts = transform_ranges(soil_to_fert, soils)
waters = transform_ranges(fert_to_water, ferts)
lights = transform_ranges(water_to_light, waters)
temps = transform_ranges(light_to_temp, lights)
humids = transform_ranges(temp_to_humid, temps)
locs = transform_ranges(humid_to_loc, humids)
for seed in seeds:
soil = next(dst + seed - src for (dst, src, len) in seed_to_soil if src <= seed < (src + len))
fert = next(dst + soil - src for (dst, src, len) in soil_to_fert if src <= soil < (src + len))
water = next(dst + fert - src for (dst, src, len) in fert_to_water if src <= fert < (src + len))
light = next(dst + water - src for (dst, src, len) in water_to_light if src <= water < (src + len))
temp = next(dst + light - src for (dst, src, len) in light_to_temp if src <= light < (src + len))
humid = next(dst + temp - src for (dst, src, len) in temp_to_humid if src <= temp < (src + len))
loc = next(dst + humid - src for (dst, src, len) in humid_to_loc if src <= humid < (src + len))
result = min(result, loc)
return result
(loc, _) = min(locs)
return loc
def part2():
result = sys.maxsize
input = fileinput.input()
for start, len in zip(*(iter(seeds),) * 2):
soils = transform_ranges(seed_to_soil, [(start, len)])
ferts = transform_ranges(soil_to_fert, soils)
waters = transform_ranges(fert_to_water, ferts)
lights = transform_ranges(water_to_light, waters)
temps = transform_ranges(light_to_temp, lights)
humids = transform_ranges(temp_to_humid, temps)
locs = transform_ranges(humid_to_loc, humids)
seeds = [int(seed) for seed in next(input).split()[1:]]
next(input)
next(input)
(loc, _) = min(locs)
result = min(result, loc)
seed_to_soil = list(parse_map(input))
soil_to_fert = list(parse_map(input))
fert_to_water = list(parse_map(input))
water_to_light = list(parse_map(input))
light_to_temp = list(parse_map(input))
temp_to_humid = list(parse_map(input))
humid_to_loc = list(parse_map(input))
return result
part1 = do_the_thing((seed, 1) for seed in seeds)
part2 = do_the_thing(zip(*(iter(seeds),) * 2))
print(part1(), part2())
print(part1, part2)