-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.py
More file actions
40 lines (37 loc) · 1.5 KB
/
Copy pathday05.py
File metadata and controls
40 lines (37 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from math import inf
sections = open("input/day05.txt").read().split("\n\n")
seeds = [int(n) for n in sections[0][sections[0].index(":")+2:].split(" ")]
maps = [
sorted(
[[int(n) for n in entry.split(" ")] for entry in section.split("\n")[1:]],
key=lambda x: x[1])
for section in sections[1:]]
min_location = inf
for element in seeds:
for ranges in maps:
for map_range in ranges:
if map_range[1] <= element and element < map_range[1] + map_range[2]:
element = map_range[0] + element - map_range[1]
break
min_location = min(min_location, element)
print(f"a = {min_location}")
min_location = inf
seed_ranges = [(seeds[i*2], seeds[i*2] + seeds[i*2+1]) for i in range(len(seeds) // 2)]
for seed_range in seed_ranges:
src_list = [seed_range]
for ranges in maps:
next_list = []
for start, stop in src_list:
for map_range in ranges:
if map_range[1] <= start and start < map_range[1] + map_range[2]:
end = min(map_range[1] + map_range[2], stop)
offset = map_range[0] - map_range[1]
next_list.append((start + offset, end + offset))
start = end
if start == stop:
break
if start != stop:
next_list.append((start, stop))
src_list = next_list
min_location = min(min_location, min(x[0] for x in src_list))
print(f"b = {min_location}")