-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.py
44 lines (32 loc) · 979 Bytes
/
day5.py
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
41
42
# Part one only
import re;
seeds = []
mappings = []
def parse_input():
global seeds
with open("input5.txt") as file:
input = file.read()
seeds = input.split(":")[1].split("\n")[0].split()
transformations = re.findall(r'map:\n([^a-zA-Z]*)(?:\n[a-zA-Z]|$)', input)
for t in transformations:
mappings.append(t.strip().split('\n'))
def transform():
global seeds
result = []
for seed in seeds:
new_seed = seed
for mapping in mappings:
new_seed = process_seed(int(new_seed), mapping)
result.append(new_seed)
return result
def process_seed(seed, mapping):
for line in mapping:
values = line.split()
dst, src, rng = int(values[0]), int(values[1]), int(values[2])
offset = rng - 1
if src <= seed <= src + offset:
seed = dst + (seed - src)
break
return seed
def lowest_location():
return min(transform())