-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrope_bridge.py
84 lines (57 loc) · 1.67 KB
/
rope_bridge.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
from utils import read_input, run
import numpy as np
FNAME = "09/input.txt"
UP = np.array([0, 1])
DOWN = np.array([0, -1])
LEFT = np.array([-1, 0])
RIGHT = np.array([1, 0])
def parse_line(line):
split = line.split(' ')
return split[0], int(split[1])
##########
# PART 1 #
##########
def direction_vector(direction):
if direction == 'U':
return UP
elif direction == 'D':
return DOWN
elif direction == 'L':
return LEFT
elif direction == 'R':
return RIGHT
raise Exception('Unknown direction')
def move_head(head, dir):
return head + direction_vector(dir)
def move_tail(tail, head):
diff = head - tail
if any(abs(diff) > 1):
return tail + np.clip(diff, -1, 1)
return tail
def part_one(input_file):
data = read_input(input_file, parse_chunk=parse_line)
head = np.array([0, 0])
tail = np.array([0, 0])
visited = {(0, 0)}
for direction, repeats in data:
for _ in range(repeats):
head = move_head(head, direction)
tail = move_tail(tail, head)
visited.add(tuple(tail))
return len(visited)
##########
# PART 2 #
##########
def part_two(input_file):
data = read_input(input_file, parse_chunk=parse_line)
rope = [np.array([0, 0]) for _ in range(10)]
visited = {(0, 0)}
for direction, repeats in data:
for _ in range(repeats):
rope[0] = move_head(rope[0], direction)
for i in range(1, len(rope)):
rope[i] = move_tail(rope[i], rope[i-1])
visited.add(tuple(rope[-1]))
return len(visited)
if __name__ == '__main__':
run(part_one, part_two, FNAME)