-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday08.py
executable file
·82 lines (62 loc) · 1.47 KB
/
day08.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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2024, 8)
fin = advent.get_input()
grid = read_char_matrix(fin)
antennas = []
for r, row in enumerate(grid):
for c, char in enumerate(row):
if char != '.':
antennas.append((r, c, char))
points = set()
for a, b in product(antennas, repeat=2):
if a == b:
continue
r1, c1, p1 = a
r2, c2, p2 = b
if p1 != p2:
continue
dr = r2 - r1
dc = c2 - c1
s = z3.Solver()
r = z3.Int('r')
c = z3.Int('c')
s.add(r == r1 + 2 * dr)
s.add(r == r2 + dr)
s.add(c == c1 + 2 * dc)
s.add(c == c2 + dc)
if s.check() == z3.sat:
m = s.model()
points.add((m.eval(r).as_long(), m.eval(c).as_long()))
s = z3.Solver()
r = z3.Int('r')
c = z3.Int('c')
s.add(r == r1 - dr)
s.add(r == r2 - 2 * dr)
s.add(c == c1 - dc)
s.add(c == c2 - 2 * dc)
if s.check() == z3.sat:
m = s.model()
points.add((m.eval(r).as_long(), m.eval(c).as_long()))
points = list(filter(lambda p: 0 <= p[0] < len(grid) and 0 <= p[1] < len(grid[0]), points))
ans1 = len(points)
advent.print_answer(1, ans1)
points = set()
for a, b in product(antennas, repeat=2):
if a == b:
continue
r1, c1, p1 = a
r2, c2, p2 = b
if p1 != p2:
continue
dr = r2 - r1
dc = c2 - c1
for m in count(0):
r = r1 + m * dr
c = c1 + m * dc
if not (0 <= r < len(grid) and 0 <= c < len(grid[0])):
break
points.add((r, c))
points = list(filter(lambda p: 0 <= p[0] < len(grid) and 0 <= p[1] < len(grid[0]), points))
ans2 = len(points)
advent.print_answer(2, ans2)