-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.py
executable file
·262 lines (200 loc) · 7.44 KB
/
day24.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from typing import Any, Optional
import sys
type Pos2D = tuple[float, float]
type Velocity2D = tuple[float, float]
type Eq = tuple[float, float]
def read_input(filename: str) -> list[tuple[Pos2D, Velocity2D]]:
data = []
with open(filename, "r") as input_file:
for line in input_file.readlines():
line = line.rstrip().split(" @ ")
pos = tuple(map(float, line[0].split(", ")))
pos2D = (pos[0], pos[1])
vel = tuple(map(float, line[1].split(", ")))
vel2D = (vel[0], vel[1])
data.append((pos2D, vel2D))
return data
def get_line_equation(p1: Pos2D, v1: Velocity2D) -> Eq:
p1_x, p1_y = p1
v1_x, v1_y = v1
p2_x = p1_x + v1_x
p2_y = p1_y + v1_y
if p2_x == p1_x:
assert False, "no vertical line expected"
return sys.float_info.max, p1_x
a = (p2_y - p1_y) / (p2_x - p1_x)
# y - p1_y = a * (x - p1_x)
# y = a * (x - p1_x) + p1_y
# y = a * x - a * p1_x + p1_y
b = -a * p1_x + p1_y
return a, b
def die(msg: str):
raise Exception(msg)
def get_two_big_points(eq: Eq, min: int, max: int) -> tuple[Pos2D, Pos2D]:
a, b = eq
if a == sys.float_info.max:
assert False, "no vertical line expected"
# return (min, min), (max, max)
ymin = a * min + b
ymax = a * max + b
return (min, ymin), (max, ymax)
def get_intersection_point(
pA: Pos2D, pB: Pos2D, pC: Pos2D, pD: Pos2D
) -> Optional[Pos2D]:
pA_x, pA_y = pA
pB_x, pB_y = pB
pC_x, pC_y = pC
pD_x, pD_y = pD
# Line AB represented as a1x + b1y = c1
a1 = pB_y - pA_y
b1 = pA_x - pB_x
c1 = a1 * (pA_x) + b1 * (pA_y)
# Line CD represented as a2x + b2y = c2
a2 = pD_y - pC_y
b2 = pC_x - pD_x
c2 = a2 * (pC_x) + b2 * (pC_y)
det = a1 * b2 - a2 * b1
if det == 0:
# The lines are parallel.
return None
else:
x = (b2 * c1 - b1 * c2) / det
y = (a1 * c2 - a2 * c1) / det
return (x, y)
def is_past_point(some: Pos2D, ref: tuple[Pos2D, Velocity2D], line_eq: Eq) -> bool:
# a, b = line_eq
some_x, some_y = some
(ref_x, ref_y), (vel_x, vel_y) = ref
# if a == sys.float_info.max:
# assert False, 'no vertical line expected'
# print(f'Line equation: y = {a:.3f} * x + {b:.3f}')
if vel_x > 0:
if vel_y > 0:
increasing = True
going_right = True
else:
increasing = False
going_right = True
else:
if vel_y > 0:
increasing = False
going_right = False
else:
increasing = True
going_right = False
# print(f'Increasing: {increasing}, going right: {going_right}')
if increasing:
if going_right:
return some_x < ref_x
else:
return some_x > ref_x
else:
if going_right:
return some_x < ref_x
else:
return some_x > ref_x
def inside(some: Pos2D, min: int, max: int) -> bool:
some_x, some_y = some
return some_x >= min and some_x <= max and some_y >= min and some_y <= max
def part1(filename: str, min_value: int, max_value: int) -> int:
data = read_input(filename)
# print(data)
lines = [get_line_equation(p, v) for p, v in data]
# print(lines)
two_points = [get_two_big_points(eq, min_value, max_value) for eq in lines]
intersect_count = 0
for idx1, (pA, pB) in enumerate(two_points):
for idx2, (pC, pD) in enumerate(two_points[idx1 + 1 :]):
id2 = idx1 + idx2 + 1
# print(f"Hailstone A: {data[idx1]}")
# print(f"Hailstone B: {data[id2]}")
if idx1 == id2:
continue
intersection = get_intersection_point(pA, pB, pC, pD)
if intersection is not None:
is_past_A = is_past_point(intersection, data[idx1], lines[idx1])
is_past_B = is_past_point(intersection, data[id2], lines[id2])
if (
not is_past_A
and not is_past_B
and inside(intersection, min_value, max_value)
):
intersect_count += 1
continue
# if intersection is None:
# print(
# f"Hailstones' paths are parallel; they never intersect."
# )
# else:
# is_past_A = is_past_point(intersection, data[idx1], lines[idx1])
# is_past_B = is_past_point(intersection, data[id2], lines[id2])
# if is_past_A:
# if is_past_B:
# print(
# "Hailstones' paths crossed in the past for both hailstones."
# )
# else:
# print(f"Hailstones' paths crossed in the past for hailstone A.")
# elif is_past_B:
# print(f"Hailstones' paths crossed in the past for hailstone B.")
# else:
# if inside(intersection, min_value, max_value):
# print(
# f"Hailstones' paths will cross inside the test area (at x={intersection[0]:.3f}, y={intersection[1]:.3f})."
# )
# intersect_count += 1
# else:
# print(
# f"Hailstones' paths will cross outside the test area (at x={intersection[0]:.3f}, y={intersection[1]:.3f})."
# )
# print()
# print(f"Total number of intersections: {intersect_count}")
return intersect_count
assert part1("./sample.txt", 7, 27) == 2
assert part1("./input.txt", 200000000000000, 400000000000000) == 31208
# print(part1("./sample.txt", 7, 27))
type Pos3D = tuple[float, float, float]
type Velocity3D = tuple[float, float, float]
def read_input_part2(filename: str) -> list[tuple[Pos3D, Velocity3D]]:
data = []
with open(filename, "r") as input_file:
for line in input_file.readlines():
line = line.rstrip().split(" @ ")
pos = tuple(map(float, line[0].split(", ")))
vel = tuple(map(float, line[1].split(", ")))
data.append((pos, vel))
return data
# def colineaires(v1: Velocity3D, v2: Velocity3D) -> bool:
# v1_x, v1_y, v1_z = v1
# v2_x, v2_y, v2_z = v2
# i = v1_x / v2_x
# j = v1_y / v2_y
# k = v1_z / v2_z
# return i == j == k
from z3 import Real, Solver
def part2(filename: str) -> int:
lines3D = read_input_part2(filename)
s = Solver()
target_x = Real("target_x")
target_y = Real("target_y")
target_z = Real("target_z")
target_vx = Real("target_vx")
target_vy = Real("target_vy")
target_vz = Real("target_vz")
lines3D = lines3D[:3] # we onlt need 3 lines to solve the problem
for idx, ((point_x, point_y, point_z), (point_vx, point_vy, point_vz)) in enumerate(
lines3D
):
t = Real(f"t{idx}")
s.add(
target_x + target_vx * t == point_x + point_vx * t,
target_y + target_vy * t == point_y + point_vy * t,
target_z + target_vz * t == point_z + point_vz * t,
)
if s.check() == "unsat":
die("Cannot solve the problem.")
m = s.model()
res = m[target_x].as_long() + m[target_y].as_long() + m[target_z].as_long()
return res
assert part2("./sample.txt") == 47
assert part2("./input.txt") == 580043851566574