-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.py
120 lines (96 loc) · 2.73 KB
/
5.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from AoCLibrary import *
with open("input5.txt") as f:
real_input = f.read()
from attrs import define, field
@define
class Conversion:
source_start: int
dest_starts_x_higher: int
source_exclusive_upper: int
def get_range(self):
return range(self.source_start, self.source_exclusive_upper)
def convert_value(self, to_convert: int):
offset = to_convert - self.source_start
return self.source_start + self.dest_starts_x_higher + offset
def main(a : str):
def seed_to_location(seed: int):
current_value = seed
for from_type in order:
for bound in all_conversions[from_type]:
if current_value in bound.get_range():
current_value = bound.convert_value(current_value)
break
#no logic needed for mapping to itself
return current_value
a = a.strip()
inp = AdventInput(data=a)
seeds = nums(inp.para[0])
all_conversions: dict[str, list[Conversion]] = dd(list)
order = []
x_to_y = {}
for section in inp.para[1:]:
key = None
for line in lines(section):
cur_line_nums = nums(line)
if cur_line_nums:
dest_start, source_start, range_len = cur_line_nums
all_conversions[key].append(Conversion(source_start, dest_start - source_start, source_start + range_len))
else:
key, _, to = line.split()[0].split("-")
x_to_y[key] = to
order.append(key)
continue
order.append(to)
def get_min_location1():
return min(seed_to_location(seed) for seed in seeds)
def part2():
lo = inf
groups = list(get_groups_size_n(seeds, 2))
print("total groups", len(groups))
for i, (start, range_len) in enu(groups):
print("starting group", i, start, range_len)
for i in range(start, start + range_len):
if i % 10_000_000 == 0:
print(i)
lo = min(lo, seed_to_location(i))
return lo
print(get_min_location1())
return part2()
samp = r"""
seeds: 79 14 55 13
seed-to-soil map:
50 98 2
52 50 48
soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15
fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4
water-to-light map:
88 18 7
18 25 70
light-to-temperature map:
45 77 23
81 45 19
68 64 13
temperature-to-humidity map:
0 69 1
1 0 69
humidity-to-location map:
60 56 37
56 93 4
""".lstrip("\n")
if samp and "r" not in sys.argv:
sample_answer = main(samp)
print("sample", sample_answer)
else:
print("no sample provided")
if "s" in sys.argv:
exit()
result = main(real_input)
if result is not None:
ans(result)