-
Notifications
You must be signed in to change notification settings - Fork 1
/
12.py
65 lines (61 loc) · 1.72 KB
/
12.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
deltas = {
'N': (0, -1),
'E': (1, 0),
'W': (-1, 0),
'S': (0, 1)
}
dirs = ['N', 'E', 'S', 'W']
def star1():
shipdir = 'E'
shippos = [0, 0]
for line in open('input.txt'):
if line[0] == 'L':
cur = dirs.index(shipdir)
dgr = int(line.strip()[1:])
cur -= dgr // 90
shipdir = dirs[cur]
elif line[0] == 'R':
cur = dirs.index(shipdir)
dgr = int(line.strip()[1:])
cur += dgr // 90
cur %= 4
shipdir = dirs[cur]
elif line[0] == 'F':
x, y = deltas[shipdir]
dist = int(line.strip()[1:])
shippos[0] += x*dist
shippos[1] += y*dist
else:
x, y = deltas[line[0]]
dist = int(line.strip()[1:])
shippos[0] += x * dist
shippos[1] += y * dist
print(abs(shippos[0]) + abs(shippos[1]))
def star2():
point = [10, -1]
shippos = [0, 0]
for line in open('input.txt'):
if line[0] == 'L':
dgr = int(line.strip()[1:])
while dgr > 0:
point = [point[1], -point[0]]
dgr -= 90
elif line[0] == 'R':
dgr = int(line.strip()[1:])
while dgr > 0:
point = [-point[1], point[0]]
dgr -= 90
elif line[0] == 'F':
dist = int(line.strip()[1:])
shippos[0] += point[0] * dist
shippos[1] += point[1] * dist
else:
x, y = deltas[line[0]]
dist = int(line.strip()[1:])
point[0] += x * dist
point[1] += y * dist
print(abs(shippos[0]) + abs(shippos[1]))
print('Star 1:')
star1()
print('Star 2:')
star2()