Simplify
This commit is contained in:
parent
6fdf2e641d
commit
89f279462e
1 changed files with 24 additions and 43 deletions
61
day05.py
61
day05.py
|
@ -1,12 +1,6 @@
|
||||||
import fileinput
|
import fileinput
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
input = fileinput.input()
|
|
||||||
|
|
||||||
seeds = [int(seed) for seed in next(input).split()[1:]]
|
|
||||||
next(input)
|
|
||||||
next(input)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_map(input):
|
def parse_map(input):
|
||||||
try:
|
try:
|
||||||
|
@ -19,15 +13,6 @@ def parse_map(input):
|
||||||
yield (0, 0, sys.maxsize)
|
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):
|
def transform_ranges(map, ranges):
|
||||||
for start, length in ranges:
|
for start, length in ranges:
|
||||||
# find the start of the range
|
# find the start of the range
|
||||||
|
@ -44,27 +29,8 @@ def transform_ranges(map, ranges):
|
||||||
yield from transform_ranges(map, [(src_end, end - src_end)])
|
yield from transform_ranges(map, [(src_end, end - src_end)])
|
||||||
|
|
||||||
|
|
||||||
def part1():
|
def do_the_thing(ranges):
|
||||||
result = sys.maxsize
|
soils = transform_ranges(seed_to_soil, ranges)
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def part2():
|
|
||||||
result = sys.maxsize
|
|
||||||
|
|
||||||
for start, len in zip(*(iter(seeds),) * 2):
|
|
||||||
soils = transform_ranges(seed_to_soil, [(start, len)])
|
|
||||||
ferts = transform_ranges(soil_to_fert, soils)
|
ferts = transform_ranges(soil_to_fert, soils)
|
||||||
waters = transform_ranges(fert_to_water, ferts)
|
waters = transform_ranges(fert_to_water, ferts)
|
||||||
lights = transform_ranges(water_to_light, waters)
|
lights = transform_ranges(water_to_light, waters)
|
||||||
|
@ -73,9 +39,24 @@ def part2():
|
||||||
locs = transform_ranges(humid_to_loc, humids)
|
locs = transform_ranges(humid_to_loc, humids)
|
||||||
|
|
||||||
(loc, _) = min(locs)
|
(loc, _) = min(locs)
|
||||||
result = min(result, loc)
|
return loc
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
print(part1(), part2())
|
input = fileinput.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))
|
||||||
|
|
||||||
|
part1 = do_the_thing((seed, 1) for seed in seeds)
|
||||||
|
part2 = do_the_thing(zip(*(iter(seeds),) * 2))
|
||||||
|
|
||||||
|
print(part1, part2)
|
||||||
|
|
Loading…
Reference in a new issue