-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec12.py
92 lines (70 loc) · 2.11 KB
/
dec12.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
#!/usr/bin/env python
from collections import namedtuple
from enum import Enum
import numpy as np
from ibidem.advent_of_code.util import get_input_name
Command = namedtuple("Command", ("action", "value"))
class Boat():
def __init__(self, pos, direction):
self.pos = pos
self.direction = direction
class Direction(Enum):
NORTH = (0, 1)
EAST = (1, 0)
SOUTH = (0, -1)
WEST = (-1, 0)
def d(self):
return np.array(self.value)
def next(self):
members = list(self.__class__)
index = members.index(self) + 1
if index >= len(members):
index = 0
return members[index]
def prev(self):
members = list(self.__class__)
index = members.index(self) - 1
return members[index]
class Action(Enum):
NORTH = "N"
EAST = "E"
SOUTH = "S"
WEST = "W"
LEFT = "L"
RIGHT = "R"
FORWARD = "F"
def direction(self):
return np.array(Direction[self.name].value)
def load():
with open(get_input_name(12, 2020)) as fobj:
commands = []
for line in fobj:
line = line.strip()
commands.append(Command(Action(line[0]), int(line[1:])))
return commands
def part1(commands):
boat = Boat(np.array((0, 0)), Direction.EAST)
for command in commands:
if command.action == Action.LEFT:
turns = command.value // 90
for _ in range(turns):
boat.direction = boat.direction.prev()
elif command.action == Action.RIGHT:
turns = command.value // 90
for _ in range(turns):
boat.direction = boat.direction.next()
elif command.action == Action.FORWARD:
distance = command.value
boat.pos += boat.direction.d() * distance
else:
distance = command.value
boat.pos += command.action.direction() * distance
result = abs(boat.pos[0]) + abs(boat.pos[1])
print(f"Manhattan distance is {result}")
return result
def part2():
pass
if __name__ == "__main__":
commands = load()
part1(commands)
part2()