-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.py
201 lines (163 loc) · 5.67 KB
/
day19.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""Solution for Advent of Code day 19."""
from collections import defaultdict
from pathlib import Path
from itertools import permutations
import doctest
import click
BeaconPosition = tuple[int]
def read_scanners(filename: Path) -> list[list]:
"""Read the raw scanner inputs.
Args:
filename (Path): filename
Returns:
list[list] scanned beacon positions for each scanner
"""
result = []
with filename.open("r") as file:
for line in file:
if "scanner" in line:
result.append([])
else:
pos = list(
map(
int,
"".join(c if c in "1234567890-" else " " for c in line).split(),
)
)
if len(pos) == 3:
result[-1].append(tuple(pos))
return result
def calculate_potential_beacon_positions(
initial_beacon_positions: list[BeaconPosition],
):
"""Creates a list of all real possible beacone positions
Scanners don`t know their rotation or facing direction. This function
calculates for possible scenarios the beacon positions.
Args:
initial_beacon_positions (list[BeaconPosition]): provided initial postions
Results:
list[list[BeaconPosition]] : all possible permutations
"""
res = []
for permutation in permutations([0, 1, 2]):
ii = [i for i in permutation if i != permutation[i]]
ks = (
[[], [0, 1], [0, 2], [1, 2]] if len(ii) != 2 else [[0], [1], [2], [0, 1, 2]]
)
for k in ks:
l = []
for pt in initial_beacon_positions:
l.append(
tuple(pt[permutation[i]] * (-1 if i in k else 1) for i in range(3))
)
res.append(l)
return res
def sub_vectors(a, b):
"""Subtracts two vectors.
Args:
pos1 (tuple[int]): first position
pos1:(tuple[int]): second position
Returns:
tuple[int]: element wise subtraction
Examples:
>>> sub_vectors((1,4,6), (1,3,7))
(0, 1, -1)
"""
return tuple(a[i] - b[i] for i in range(3))
def add_vectors(pos1: tuple[int], pos2: tuple[int]) -> tuple[int]:
"""Adds two vectors.
Args:
pos1 (tuple[int]): first position
pos1:(tuple[int]): second position
Returns:
tuple[int]: element wise sum
Examples:
>>> add_vectors((1,4,6), (1,3,7))
(2, 7, 13)
"""
return tuple(pos1[i] + pos2[i] for i in range(3))
def manhatten_distance(pos1: tuple[int], pos2: tuple[int]) -> int:
"""Calculate the manhattan distance for 2 points.
Args:
pos1 (tuple[int]): first position
pos1:(tuple[int]): second position
Returns:
int: manhattan distance
Examples:
>>> manhatten_distance((1105,-1205,1229), (-92,-2380,-20))
3621
"""
return sum(abs(x) for x in sub_vectors(pos1, pos2))
def process_scanners(scanner_input: list[list]):
"""process the scanner inputs."""
# [axis, scanner_pos, beacons]
found = [(0, (0, 0, 0), scanner_input[0])]
for _, _, scanned_beacon_positions in found:
for axis, local_beacon_postions in enumerate(scanner_input):
if axis not in [f[0] for f in found]:
for (
potential_local_beacon_postions
) in calculate_potential_beacon_positions(local_beacon_postions):
diffs = defaultdict(list)
for i, pos_original in enumerate(scanned_beacon_positions):
for j, pos_found in enumerate(potential_local_beacon_postions):
diffs[sub_vectors(pos_original, pos_found)].append((i, j))
for potential_scanner_pos in diffs:
if len(diffs[potential_scanner_pos]) >= 12:
found.append(
(
axis,
potential_scanner_pos,
[
add_vectors(y, potential_scanner_pos)
for y in potential_local_beacon_postions
],
)
)
break
else:
continue
break
return found
@click.group()
def main():
"""CLI for the solution of day 19
Advent of code 2021 (https://adventofcode.com/2021/day/19)
"""
@main.command()
@click.argument(
"filename",
required=False,
type=Path,
default=Path("test_data/day_19.data"),
)
def part_1(filename: Path):
"""Part one of day 19. (num of beacons)"""
found = process_scanners(read_scanners(filename))
found_beacons = set()
for _, _, beacons in found:
found_beacons = found_beacons.union(set(beacons))
print(f"{len(found_beacons)} beacons found.")
@main.command()
@click.argument(
"filename",
required=False,
type=Path,
default=Path("test_data/day_19.data"),
)
def part_2(filename: Path):
"""Part two of day 19. (max distance)"""
found = process_scanners(read_scanners(filename))
max_manhattan_dist = 0
for _, scanner_pos, _ in found:
for _, scanner_pos_2, _ in found:
max_manhattan_dist = max(
[max_manhattan_dist, manhatten_distance(scanner_pos, scanner_pos_2)]
)
print(f"The max manhattan distance between two scanners is {max_manhattan_dist}")
@main.command()
def test():
"""run doctest."""
print(doctest.testmod())
if __name__ == "__main__":
main()