-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday01.py
More file actions
52 lines (38 loc) · 926 Bytes
/
day01.py
File metadata and controls
52 lines (38 loc) · 926 Bytes
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
import math
def get_input():
with open('test.txt') as f:
data = f.read()
data = data.strip().split(',')
data = [s.strip() for s in data]
return data
def vec_add(v1, v2):
x1, y1 = v1
x2, y2 = v2
return [x1 + x2, y1 + y2]
def manh_dist(v1, v2):
x1, y1 = v1
x2, y2 = v2
return abs(x1 - x2) +abs(y1 - y2)
def part1(data):
cur_pos = [0, 0]
cur_face = 0
dirs = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
]
for item in data:
d, step = item[0], int(item[1:])
if d == 'L':
cur_face = (cur_face - 1) % 4
else:
cur_face = (cur_face + 1) % 4
idx = (cur_face + 1) % 2
new_vec = dirs[cur_face]
new_vec[idx] = new_vec[idx] * step
cur_pos = vec_add(cur_pos, new_vec)
print(cur_pos)
print(manh_dist([0, 0], cur_pos))
data = get_input()
part1(data)