-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
129 lines (103 loc) · 3.41 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from __future__ import annotations
from typing import List, NamedTuple, Tuple
class Instruction(NamedTuple):
action: str
value: int
@staticmethod
def parse(line: str) -> Instruction:
return Instruction(line[0], int(line[1:]))
DIRECTIONS = ["N", "E", "S", "W"]
class Ship:
def __init__(self):
self.x: int = 0
self.y: int = 0
self.direction: str = "E"
def process_instruction(self, instr: Instruction) -> None:
if instr.action == "N":
self.y += instr.value
elif instr.action == "S":
self.y -= instr.value
elif instr.action == "E":
self.x += instr.value
elif instr.action == "W":
self.x -= instr.value
elif instr.action == "R":
self.direction = DIRECTIONS[
(DIRECTIONS.index(self.direction) + instr.value // 90) % 4
]
elif instr.action == "L":
self.direction = DIRECTIONS[
(DIRECTIONS.index(self.direction) - instr.value // 90) % 4
]
elif instr.action == "F":
if self.direction == "N":
self.y += instr.value
elif self.direction == "S":
self.y -= instr.value
elif self.direction == "E":
self.x += instr.value
elif self.direction == "W":
self.x -= instr.value
def manhattan(self) -> int:
return abs(self.x) + abs(self.y)
class ShipAndWaypoint:
def __init__(self):
self.x: int = 0
self.y: int = 0
self.direction: str = "E"
self.waypoint_x: int = 10
self.waypoint_y: int = 1
def process_instruction(self, instr: Instruction) -> None:
if instr.action == "N":
self.waypoint_y += instr.value
elif instr.action == "S":
self.waypoint_y -= instr.value
elif instr.action == "E":
self.waypoint_x += instr.value
elif instr.action == "W":
self.waypoint_x -= instr.value
elif instr.action == "R":
# x,y = y,-x
for _ in range(instr.value // 90):
self.waypoint_x, self.waypoint_y = self.waypoint_y, -self.waypoint_x
elif instr.action == "L":
# x,y = -y,x
for _ in range(instr.value // 90):
self.waypoint_x, self.waypoint_y = -self.waypoint_y, self.waypoint_x
elif instr.action == "F":
self.x += instr.value * self.waypoint_x
self.y += instr.value * self.waypoint_y
def manhattan(self) -> int:
return abs(self.x) + abs(self.y)
def parse_raw(raw: str) -> List[Instruction]:
return [Instruction.parse(line) for line in raw.split("\n")]
# TESTS
RAW = """F10
N3
F7
R90
F11"""
# lst = parse_raw(RAW)
# s = Ship()
# for instr in lst:
# s.process_instruction(instr)
# print(s.x, s.y)
# print(s.manhattan())
# s2 = ShipAndWaypoint()
# for instr in lst:
# s2.process_instruction(instr)
# print(s2.x, s2.y, s2.waypoint_x, s2.waypoint_y)
# print(s2.manhattan())
# PUZZLE
with open("inputs/day12.txt") as f:
data = [Instruction.parse(line) for line in f.read().split("\n")]
my_ship = Ship()
for instr in data:
my_ship.process_instruction(instr)
print(my_ship.x, my_ship.y)
print(my_ship.manhattan())
my_ship2 = ShipAndWaypoint()
for instr in data:
my_ship2.process_instruction(instr)
print(my_ship2.x, my_ship2.y)
print(my_ship2.manhattan())