-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (106 loc) · 3.35 KB
/
main.py
File metadata and controls
134 lines (106 loc) · 3.35 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import re
test = """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
"""
location_numbers = []
def ints(s: str) -> list[int]:
"""
:param s: (str): The input string to search for integers.
:return: List[int]: A list of integers found in the input string.
"""
"""
Return a list of integers found in the input string.
Example:
>>> ints("seeds: 3640772818 104094365 1236480411 161072229 376099792")
[3640772818, 104094365, 1236480411, 161072229, 376099792]
"""
return [int(m) for m in re.findall(r"-?[\d]+", s)]
data = open("../inputs/day05.txt", "r").read()
parts = data.split("\n\n")
seed_names, map_sets = parts[0], parts[1:]
seeds = ints(parts[0])
min_location = 2**63 - 1 # max int val
for seed in seeds:
location = seed
for map_set in map_sets:
for line in map_set.split("\n")[1:]:
dst_start, src_start, rng = ints(line)
if location in range(src_start, src_start + rng):
location = dst_start + (location - src_start)
break
min_location = min(min_location, location)
print(f"{min_location=}")
# first part 214922730
content = [line.strip() for line in data.splitlines()]
currents = []
nexts = ints(content[0])
unmapped = []
cleaned_content = content[2:]
cleaned_content = [i for i in cleaned_content if i != ""]
for line in cleaned_content:
numbers = ints(line)
if not numbers:
currents = nexts
currents.extend(unmapped)
nexts, unmapped = [], []
else:
currents.extend(unmapped)
unmapped = []
current_destination, current_source, range_map = numbers[:3]
while len(currents) > 0:
if (
currents[0] < current_source + range_map
and currents[0] + currents[1] > current_source
):
to_map_start = max(currents[0], current_source)
to_map_end = min(current_source + range_map, currents[0] + currents[1])
to_map_range = to_map_end - to_map_start
mapped_start = current_destination + (to_map_start - current_source)
mapped_range = to_map_range
nexts.append(mapped_start)
nexts.append(mapped_range)
if currents and currents[0] < to_map_start:
left_start = currents[0]
left_end = current_source
left_range = left_end - left_start
currents.extend([left_start, left_range])
if currents and currents[0] + currents[1] > to_map_end:
right_start = current_source + range_map
right_end = currents[0] + currents[1]
right_range = right_end - right_start
currents.extend([right_start, right_range])
else:
unmapped.extend(currents[:2])
currents.pop(0)
currents.pop(0)
currents = nexts
currents.extend(unmapped)
nexts = []
unmapped = []
actuals = [currents[i] for i in range(0, len(currents), 2)]
print(min(actuals))
# second part 148041808