-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
77 lines (68 loc) · 2.15 KB
/
day12.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
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 12 16:04:12 2020
@author: rundx
"""
import numpy as np
f = open('day12.txt','r')
dirs = ['E','S','W','N']
facing = 'E'
position = [0,0]
orders = [line.rstrip() for line in f]
'''
for order in orders:
if order[0] == 'R':
print(facing, order[1:])
facing = dirs[(dirs.index(facing) + int(order[1:])//90) % 4]
print(facing)
elif order[0] == 'L':
facing = dirs[(dirs.index(facing) - int(order[1:])//90) % 4]
elif order[0] == 'F':
direction = facing
if direction == 'N':
position[1] += int(order[1:])
elif direction == 'E':
position[0] += int(order[1:])
elif direction == 'S':
position[1] -= int(order[1:])
elif direction == 'W':
position[0] -= int(order[1:])
else:
direction = order[0]
if direction == 'N':
position[1] += int(order[1:])
elif direction == 'E':
position[0] += int(order[1:])
elif direction == 'S':
position[1] -= int(order[1:])
elif direction == 'W':
position[0] -= int(order[1:])
print(position)
'''
waypoint = [10,1]
for order in orders:
if order[0] == 'R':
theta = np.radians(360-int(order[1:]))
cos = np.cos(theta)
sin = np.sin(theta)
M = np.round(np.array(((cos,-sin),(sin,cos))))
waypoint = list(np.round(np.matmul(M,waypoint)))
elif order[0] == 'L':
theta = np.radians(int(order[1:]))
cos = np.cos(theta)
sin = np.sin(theta)
M = np.round(np.array(((cos,-sin),(sin,cos))))
waypoint = list(np.round(np.matmul(M,waypoint)))
elif order[0] == 'F':
position[0],position[1] = position[0]+int(order[1:])*waypoint[0], position[1]+int(order[1:])*waypoint[1]
else:
direction = order[0]
if direction == 'N':
waypoint[1] += int(order[1:])
elif direction == 'E':
waypoint[0] += int(order[1:])
elif direction == 'S':
waypoint[1] -= int(order[1:])
elif direction == 'W':
waypoint[0] -= int(order[1:])
print(position)