Skip to content

Commit 31e4542

Browse files
committed
fix algorithm
1 parent 9756e62 commit 31e4542

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

aoc_wim/aoc2017/q20.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
p=<3,0,0>, v=<-1,0,0>, a=<0,0,0>
1616
"""
1717

18-
template = 'p=<{:d},{:d},{:d}>, v=<{:d},{:d},{:d}>, a=<{:d},{:d},{:d}>'
18+
template = "p=<{:d},{:d},{:d}>, v=<{:d},{:d},{:d}>, a=<{:d},{:d},{:d}>"
1919

2020

2121
def parsed(data):
@@ -26,10 +26,27 @@ def key(p):
2626
return [abs(x) + abs(y) + abs(z) for x, y, z in chunks(reversed(p), 3)]
2727

2828

29+
def abs_accel(p):
30+
px, py, pz, vx, vy, vz, ax, ay, az = p
31+
a = abs(ax) + abs(ay) + abs(az)
32+
return a
33+
34+
35+
def pos(p0, t):
36+
px0, py0, pz0, vx0, vy0, vz0, ax0, ay0, az0 = p0
37+
px1 = px0 + vx0 * t + (ax0 * t ** 2) // 2
38+
py1 = py0 + vy0 * t + (ay0 * t ** 2) // 2
39+
pz1 = pz0 + vz0 * t + (az0 * t ** 2) // 2
40+
d = abs(px1) + abs(py1) + abs(pz1)
41+
return d
42+
43+
2944
def part_a(data):
3045
particles = parsed(data)
3146
p_min = min(particles, key=key)
32-
return particles.index(p_min)
47+
a_min = abs_accel(p_min)
48+
d = {i: pos(p, 100) for i, p in enumerate(particles) if abs_accel(p) == a_min}
49+
return min(d, key=d.get)
3350

3451

3552
def part_b(data):

0 commit comments

Comments
 (0)