Day 09
This commit is contained in:
@@ -20,7 +20,7 @@ for y in range(len(forest)):
|
|||||||
pos += dir
|
pos += dir
|
||||||
score *= dir_score
|
score *= dir_score
|
||||||
max_score = max(max_score, score)
|
max_score = max(max_score, score)
|
||||||
print(max_score)
|
print(max_score)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
2000
day09/input.txt
Normal file
2000
day09/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
25
day09/part1.py
Normal file
25
day09/part1.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
lines = (x.strip() for x in open("input.txt"))
|
||||||
|
positions = set()
|
||||||
|
LEN = 2
|
||||||
|
rope = [np.array((0, 0)) for i in range(LEN)]
|
||||||
|
dirs = {
|
||||||
|
"U": np.array((1, 0)),
|
||||||
|
"D": np.array((-1, 0)),
|
||||||
|
"L": np.array((0, -1)),
|
||||||
|
"R": np.array((0, 1)),
|
||||||
|
}
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
d, i = line.split(" ")
|
||||||
|
d, i = dirs[d], int(i)
|
||||||
|
for _ in range(i):
|
||||||
|
rope[0] += d
|
||||||
|
for p in range(1, 2):
|
||||||
|
diff = rope[p-1]-rope[p]
|
||||||
|
rope[p] += 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))
|
||||||
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