-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_new.py
67 lines (46 loc) · 1.38 KB
/
5_new.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from enum import Enum, auto
from functools import reduce
DEBUG = 1
N = open('./in/5.test.txt').read().split('\n\n')
# N = open('./in/5.txt').read().split('\n\n')
class Resource(Enum):
SEED = auto()
SOIL = auto()
FERTILIZER = auto()
WATER = auto()
LIGHT = auto()
TEMPERATURE = auto()
HUMIDITY = auto()
LOCATION = auto()
resources = list(Resource)
_seeds, *maps = N
ranges = {}
for src, dest, chunk in zip(resources, resources[1:], maps):
_header, *lines = chunk.splitlines()
if DEBUG:
assert _header.upper() == f'{src.name}-TO-{dest.name} MAP:'
ranges[src, dest] = []
for line in lines:
dest_start, src_start, length = map(int, line.split())
delta = dest_start - src_start
ranges[src, dest].append((range(src_start, src_start + length), delta))
def naive_range(src: Resource, dest: Resource, n: int):
for rg, delta in ranges[src, dest]:
if n in rg:
return n + delta
return n
def fold(n, convert=naive_range):
return reduce(
lambda acc, res: (res, convert(acc[0], res, acc[1])),
resources[1:],
(Resource.SEED, n))[1]
def part_1():
seeds = list(map(int, _seeds.split()[1:]))
return min(map(fold, seeds))
def part_2():
pass
if __name__ == '__main__':
print('--- Part 1 ---')
print(part_1())
print('\n--- Part 2 ---')
print(part_2())