-
Notifications
You must be signed in to change notification settings - Fork 1
/
day12_ship.py
131 lines (103 loc) · 3.84 KB
/
day12_ship.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
130
131
from typing import List, NamedTuple
DIRECTION_MAPPING = {"N": 0, "E": 90, "S": 180, "W": 270}
DEGREE_MAPING = {value: key for key, value in DIRECTION_MAPPING.items()}
class Action(NamedTuple):
type: str
value: int
class Ship:
def __init__(self, initial_direction):
self.direction = DIRECTION_MAPPING[initial_direction]
self.north_south = 0
self.east_west = 0
def __repr__(self):
return f"Ship(NS={self.north_south},EW={self.east_west})"
def perform_actions(self, actions: List[Action]):
for action in actions:
self.move(action.type, action.value)
def move(self, action, value):
if action == "N":
self.north_south += value
elif action == "S":
self.north_south -= value
elif action == "E":
self.east_west += value
elif action == "W":
self.east_west -= value
elif action == "L":
self.direction = (self.direction - value) % 360
elif action == "R":
self.direction = (self.direction + value) % 360
elif action == "F":
direction = DEGREE_MAPING[self.direction]
self.move(direction, value)
else:
raise ValueError("should not get here")
@property
def manhattan_distance(self):
return abs(self.north_south) + abs(self.east_west)
TEST_INPUT = """F10
N3
F7
R90
F11"""
def test_ship_part_1():
ship = Ship("E")
actions = [Action(value[:1], int(value[1:])) for value in TEST_INPUT.split("\n")]
ship.perform_actions(actions)
assert ship.manhattan_distance == 25
class Waypoint:
def __init__(self, east_offset, north_offset, ship):
self.north_south = north_offset
self.east_west = east_offset
self.ship = ship
def __repr__(self):
return f"Waypoint(NS={self.north_south},EW={self.east_west})"
def perform_actions(self, actions: List[Action]):
for action in actions:
self.move(action.type, action.value)
def move(self, action, value):
if action == "N":
self.north_south += value
elif action == "S":
self.north_south -= value
elif action == "E":
self.east_west += value
elif action == "W":
self.east_west -= value
elif action == "L" or action == "R":
if action == "L":
degrees = value
else:
degrees = 360 - value
if degrees == 90:
self.north_south, self.east_west = self.east_west, -self.north_south
elif degrees == 180:
self.north_south, self.east_west = -self.north_south, -self.east_west
elif degrees == 270:
self.north_south, self.east_west = -self.east_west, self.north_south
else:
raise ValueError("cannot be here")
elif action == "F":
self.ship.move("N", value * self.north_south)
self.ship.move("E", value * self.east_west)
else:
raise ValueError("should not get here")
@property
def manhattan_distance(self):
return abs(self.north_south) + abs(self.east_west)
def test_ship_part_2():
ship = Ship("E")
waypoint = Waypoint(10, 1, ship=ship)
actions = [Action(value[:1], int(value[1:])) for value in TEST_INPUT.split("\n")]
waypoint.perform_actions(actions)
assert ship.manhattan_distance == 286
if __name__ == "__main__":
with open("2020/data/day12_input.txt") as f:
actions = [Action(value[:1], int(value[1:])) for value in f.readlines()]
ship = Ship("E")
ship.perform_actions(actions)
print(f"Part 1 answer is {ship.manhattan_distance}")
ship = Ship("E")
waypoint = Waypoint(10, 1, ship=ship)
waypoint.perform_actions(actions)
print(f"Part 2 answer is {ship.manhattan_distance}")