-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem5.py
84 lines (58 loc) · 2.08 KB
/
problem5.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 5 01:20:16 2023
@author: avicenna
"""
import time
from utils import PointedInterval
from interval import interval as _int
def solve(path, part):
s = lambda x: x.strip().strip('\n')
with open(path, "r") as fp:
lines = list(map(s, fp.readlines()))
if part==2:
seed_info = list(map(int,lines[0].split(': ')[1].split(' ')))
seed_interval = _int(*[[int(a), int(a+b)] for a,b in
zip(seed_info[::2], seed_info[1::2])])
elif part==1:
seed_interval = _int(*list(map(int,lines[0].split(': ')[1].split(' '))))
I = [ind+2 for ind,line in enumerate(lines[2:]) if line == '']
if lines[2:] != '': I = [1] + I
if lines[-1] != '': I = I + [len(lines)]
maps = [lines[i0+1:i1] for i0,i1 in zip(I[:-1],I[1:])]
keys = ['seed_to_soil', 'soil_to_fertilizer', 'fertilizer_to_water',
'water_to_light', 'light_to_temperature', 'temperature_to_humidity',
'humidity_to_location']
v = {key:[] for key in keys}
m = {key:[] for key in keys}
for info in maps:
key = info[0].split(' ')[0].replace('-','_')
for data in info[1:]:
d0 = int(data.split(' ')[0])
s0 = int(data.split(' ')[1])
n = int(data.split(' ')[2])
m[key].append([s0, s0+n-1])
v[key].append(d0)
for key in m:
m[key] = PointedInterval(m[key], v[key])
for key in m:
seed_interval = m[key].map_interval(seed_interval)
return int(seed_interval[0].inf)
if __name__ == "__main__":
t0 = time.time()
answer = solve("./inputs/test_input5_1.txt", 1)
t1 = time.time()
print(f"part1 test answer is {answer} ({t1-t0:.2f} seconds)")
t0 = time.time()
answer = solve("./inputs/input5.txt", 1)
t1 = time.time()
print(f"part1 answer is {answer} ({t1-t0:.2f} seconds)")
t0 = time.time()
answer = solve("./inputs/test_input5_1.txt", 2)
t1 = time.time()
print(f"part2 test answer is {answer} ({t1-t0:.2f} seconds)")
t0 = time.time()
answer = solve("./inputs/input5.txt", 2)
t1 = time.time()
print(f"part2 answer is {answer} ({t1-t0:.2f} seconds)")