-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.py
91 lines (83 loc) · 2.38 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
inp_f = open("input/day12.txt", "r")
inp = inp_f.read()
inp_list = [line for line in inp.split("\n") if line != ""]
def part1():
ns_pos = 0
ew_pos = 0
facing = "E"
for instr in inp_list:
action = instr[0]
value = int(instr[1:])
if action == "N":
ns_pos += value
if action == "S":
ns_pos -= value
if action == "E":
ew_pos += value
if action == "W":
ew_pos -= value
if action == "L":
while value > 0:
if facing == "N":
facing = "W"
elif facing == "W":
facing = "S"
elif facing == "S":
facing = "E"
elif facing == "E":
facing = "N"
value -= 90
if action == "R":
while value > 0:
if facing == "N":
facing = "E"
elif facing == "E":
facing = "S"
elif facing == "S":
facing = "W"
elif facing == "W":
facing = "N"
value -= 90
if action == "F":
if facing == "N":
ns_pos += value
if facing == "E":
ew_pos += value
if facing == "S":
ns_pos -= value
if facing == "W":
ew_pos -= value
return abs(ns_pos) + abs(ew_pos)
def part2():
ship = [0, 0]
wp = [1, 10]
for instr in inp_list:
action = instr[0]
value = int(instr[1:])
if action == "N":
wp[0] += value
if action == "S":
wp[0] -= value
if action == "E":
wp[1] += value
if action == "W":
wp[1] -= value
if action == "L":
while value > 0:
old_wp = wp.copy()
wp[1] = -old_wp[0]
wp[0] = old_wp[1]
value -= 90
if action == "R":
while value > 0:
old_wp = wp.copy()
wp[1] = old_wp[0]
wp[0] = -old_wp[1]
value -= 90
if action == "F":
ship[0] += wp[0] * value
ship[1] += wp[1] * value
return abs(ship[0]) + abs(ship[1])
if __name__ == "__main__":
# print(part1())
print(part2())