-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
57 lines (44 loc) · 1.65 KB
/
day2.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
from common.day import Day
class Day2(Day):
UP = "up"
DOWN = "down"
FORWARD = "forward"
def __init__(self, input_file_name: str):
super().__init__(input_file_name)
self.movement_list = self.get_movement_list()
def get_movement_list(self):
return list(map(lambda x: Day2.split_and_convert(x), self.read_input_lines()))
@staticmethod
def split_and_convert(line: str) -> (str, int):
move = line.split(" ")
return move[0], int(move[1])
def part1(self) -> int:
depth, horizontal_position = self.compute_positions_part1()
return depth * horizontal_position
def part2(self) -> int:
depth, horizontal_position = self.compute_positions_part2()
return depth * horizontal_position
def compute_positions_part1(self):
depth = 0
horizontal_position = 0
for direction, distance in self.movement_list:
if direction == self.UP:
depth -= distance
elif direction == self.DOWN:
depth += distance
elif direction == self.FORWARD:
horizontal_position += distance
return depth, horizontal_position
def compute_positions_part2(self):
depth = 0
horizontal_position = 0
aim = 0
for direction, distance in self.movement_list:
if direction == self.UP:
aim -= distance
elif direction == self.DOWN:
aim += distance
elif direction == self.FORWARD:
horizontal_position += distance
depth += (aim * distance)
return depth, horizontal_position