From 8433c88147fac68ea730e865367bf9fe3d9c7d3c Mon Sep 17 00:00:00 2001 From: Sijmen Date: Wed, 6 Dec 2023 01:42:20 +0100 Subject: [PATCH] Simplify day 5 further --- day05.py | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/day05.py b/day05.py index fbf9343..fbe3df1 100644 --- a/day05.py +++ b/day05.py @@ -33,34 +33,24 @@ def transform_ranges(map, ranges): yield from transform_ranges(map, [(src_end, end - src_end)]) -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) - - (loc, _) = min(locs) - return loc +def do_the_thing(maps, ranges): + for map in maps: + ranges = transform_ranges(map, ranges) + return min(ranges)[0] -input = fileinput.input() +def main(): + input = fileinput.input() -seeds = [int(seed) for seed in next(input).split()[1:]] -next(input) -next(input) + seeds = [int(seed) for seed in next(input).split()[1:]] + next(input) + next(input) -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)) + maps = [list(parse_map(input)) for _ in range(7)] -part1 = do_the_thing((seed, 1) for seed in seeds) -part2 = do_the_thing(zip(*(iter(seeds),) * 2)) + part1 = do_the_thing(maps, ((seed, 1) for seed in seeds)) + part2 = do_the_thing(maps, zip(*[iter(seeds)] * 2)) -print(part1, part2) + print(part1, part2) + +main()