Day 09
This commit is contained in:
18
day09/part2.py
Normal file
18
day09/part2.py
Normal file
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
import numpy as np
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
positions = set()
|
||||
rope = [np.array((0, 0)) for _ in range(10)]
|
||||
dirs = {d: p for d, p in zip("UDLR", ((1, 0), (-1, 0), (0, -1), (0, 1)))}
|
||||
|
||||
for line in lines:
|
||||
d, n = dirs[line[0]], int(line[2:])
|
||||
for _ in range(n):
|
||||
rope[0] += d
|
||||
for i in range(1, len(rope)):
|
||||
diff = rope[i-1] - rope[i]
|
||||
rope[i] += np.sign(diff) if sum(np.abs(diff)) > 2 else np.sign(diff - np.sign(diff))
|
||||
positions.add(tuple(rope[len(rope)-1]))
|
||||
|
||||
print(len(positions))
|
||||
Reference in New Issue
Block a user