Solve day 12 part 1

This commit is contained in:
Gabriel Augendre 2020-12-12 19:13:24 +01:00
parent 1360952c72
commit 37e8ea40f5
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
3 changed files with 895 additions and 0 deletions

104
2020/day12_navigation.py Normal file
View file

@ -0,0 +1,104 @@
import enum
import functools
from typing import List
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
print(f"\n+ Running on {filename}")
with open(filename) as f:
instructions = f.read().strip().split("\n")
counter_part_1 = solve_part_1(instructions)
counter_part_2 = 0
print(f"1. Found {counter_part_1}")
if expected_part_1:
assert expected_part_1 == counter_part_1
print(f"2. Found {counter_part_2}")
if expected_part_2:
assert expected_part_2 == counter_part_2
def solve_part_1(instructions: List[str]):
ship = Ship()
ship.apply_instructions(instructions)
return ship.distance_from_origin
class Ship:
def __init__(self):
self.facing = Direction.EAST
self.movements = {
Direction.NORTH: 0,
Direction.SOUTH: 0,
Direction.EAST: 0,
Direction.WEST: 0,
}
def apply_instructions(self, instructions):
for instruction in instructions:
self.apply_instruction(instruction)
@property
def distance_from_origin(self):
return abs(
self.movements[Direction.NORTH] - self.movements[Direction.SOUTH]
) + abs(self.movements[Direction.EAST] - self.movements[Direction.WEST])
def apply_instruction(self, instruction):
letter = instruction[0]
value = int(instruction[1:])
if letter in Direction.values():
direction = Direction(letter)
self.move(direction, value)
elif letter == "F":
self.move(self.facing, value)
elif letter in ["R", "L"]:
if letter == "L":
value = -value
self.facing = self.facing.turn(value)
def move(self, direction: "Direction", value: int):
self.movements[direction] += value
class Direction(enum.Enum):
NORTH = "N"
SOUTH = "S"
EAST = "E"
WEST = "W"
@staticmethod
@functools.lru_cache(maxsize=1)
def values():
return {e.value for e in Direction}
def turn(self, angle):
if angle == 0:
return self
if self == Direction.NORTH:
if angle > 0:
return Direction.EAST.turn(angle - 90)
else:
return Direction.WEST.turn(angle + 90)
if self == Direction.SOUTH:
if angle > 0:
return Direction.WEST.turn(angle - 90)
else:
return Direction.EAST.turn(angle + 90)
if self == Direction.EAST:
if angle > 0:
return Direction.SOUTH.turn(angle - 90)
else:
return Direction.NORTH.turn(angle + 90)
if self == Direction.WEST:
if angle > 0:
return Direction.NORTH.turn(angle - 90)
else:
return Direction.SOUTH.turn(angle + 90)
if __name__ == "__main__":
main("inputs/day12-test1", 25)
main("inputs/day12")

786
2020/inputs/day12 Normal file
View file

@ -0,0 +1,786 @@
S2
W5
F20
E3
S5
R90
W5
F48
R180
E3
S3
E5
S3
F83
S1
W5
F81
W3
R90
F88
S2
R90
E2
L90
W4
F77
E1
R90
S4
E2
F89
N2
L90
N2
N2
E3
L180
N1
F82
R90
S3
F64
W1
R180
S2
R180
F28
L180
S3
F100
E1
S4
S5
R90
F3
W5
N4
F9
N5
E4
R90
F83
L180
W3
N5
W2
R180
W1
S4
L90
S5
R90
R90
F36
L180
W1
F88
E3
R90
F13
W1
F13
W4
R90
E3
F98
S4
E5
R90
F77
E5
L180
N5
R90
E1
F70
L180
F54
E5
F9
L180
N3
F77
E2
F81
E2
S1
F41
L90
F32
S3
F66
N2
E4
R90
F80
R90
W1
R90
S3
F65
S4
R90
W4
S4
R90
W4
S2
E1
F44
R90
F53
E3
F67
S5
L90
R90
W2
S2
E3
N5
F97
N3
E1
R90
W4
N5
S4
F38
N5
N1
W4
S3
E1
S3
F87
E1
R90
E2
L180
S1
L90
F29
R90
S2
F95
N2
E5
F24
W1
F71
E3
L90
F16
L90
S5
L90
W1
F95
L90
W2
F97
E4
R90
N5
F77
E2
L90
F71
L90
W2
R90
F75
E1
S1
F28
F86
R90
E3
F9
N2
W4
S1
L90
F10
W4
L90
S5
E1
L90
W2
S3
F41
L90
S5
F73
L90
S4
F80
W4
R180
S4
E3
F89
E5
N5
L90
F66
N4
W1
F93
W1
F27
F68
N4
F23
L90
F14
S3
F74
N2
E2
R90
S4
F85
N2
L180
L180
F55
S5
E5
F42
N2
R90
F53
S4
F70
E2
F73
S4
F71
L90
E1
S3
E5
N3
E2
N5
L270
F69
E5
F30
N1
R90
N4
F15
E4
R180
F12
E2
F95
E2
S4
F13
S1
E1
N4
E3
L270
E4
S5
E3
N2
R180
S2
W2
L90
S3
W4
R90
E1
L180
W5
F23
E5
F67
R90
N2
W4
L90
S1
L90
F17
W2
F80
E1
F13
S2
E3
S4
F46
F70
R180
E3
L90
E4
L90
S3
F31
W5
R90
F21
S5
L90
F12
L90
F70
S1
R180
W5
F14
S5
W5
S1
F56
L90
F43
S4
N4
E1
R180
S1
R90
E3
N4
R90
S2
E5
S4
W4
R90
F88
S4
R90
F79
L90
N2
E3
F74
L90
F72
W1
N3
R90
E2
F38
W3
L90
E3
N2
R90
E2
L90
N3
E1
F96
E3
S4
R180
F28
S2
F93
L270
N3
R90
N2
E4
S1
F47
L270
F9
N2
F80
S4
L180
N5
W2
L90
W4
N5
L90
F82
R90
N3
F4
R90
F96
W5
R90
F28
W2
S2
F16
S4
R90
F9
N5
W4
F78
E3
F84
L90
F42
W2
F43
W4
L90
F95
E1
R90
F59
L90
F82
W1
F17
S4
R180
F91
L180
N1
R90
N3
F57
L90
F90
N3
R90
F98
N5
R90
N2
R90
F4
S2
F80
S2
N2
W4
L90
E2
F67
R90
W4
L180
F22
L90
S3
F9
N5
W2
L180
S3
F23
S4
W4
F90
S2
F32
S3
E5
R90
E1
F64
R180
F17
E3
R90
N2
E1
F90
W3
N5
F19
S5
E4
N5
R90
W1
F18
R180
E2
R90
F76
L90
N2
F96
F38
R180
F94
S4
W4
S4
F63
E4
L90
F69
S2
W3
N1
L270
N1
L90
N4
R90
E5
F67
F85
S1
F2
F68
W2
R90
E4
R90
S3
W4
S2
N5
F13
R180
W1
F31
S1
F23
S1
R90
F86
L90
N5
W3
F47
R90
R270
S1
L90
S1
F40
N4
R180
N1
L180
W3
S1
W5
L90
W5
F41
S4
E2
N5
E4
N1
F97
L90
F62
N5
R90
F98
R180
S5
L270
N5
W5
N4
E3
F38
L90
S2
F79
R270
E1
F14
W1
F38
E3
L180
N4
L90
S5
F90
S1
F7
N1
F11
W4
R180
F73
L90
F54
F25
W5
S3
F39
E2
F37
E2
S4
F94
W5
S2
L180
W3
F70
S3
R180
W1
N5
R90
S4
F12
S3
F45
E1
N3
R90
S2
L180
E4
S3
W5
F9
S4
R90
F19
W3
N4
W5
R90
F16
W3
S4
W1
S3
R180
S4
E4
N1
W3
F10
N5
E2
F61
E4
S1
W3
N5
W2
L90
W5
N2
W1
F8
W4
S5
L180
F76
N1
R180
S3
R180
S3
E1
R90
W1
F38
R90
F74
N2
F60
L90
E2
S3
F78
W1
F57
R90
F39
N5
F2
E3
R90
F81
E2
F2
S3
R90
F95
S4
F1
E2
N5
L270
F59
W2
R90
S1
F42
E2
N5
E4
L90
W1
F17
E2
F91
R90
E3
F30
S1
R90
W5
F74
W5
N1
N4
R180
S5
R180
F92
N4
N3
E3
R90
F60
S4
L90
E4
R270
F3
E1
L180
E1
S5
F84
E3
L90
N3
F27
W1
F58
N1
F93
N2
F30
L90
N2
L90
N2
F99
W4
S2
F13
L90
N3
W4
F100

5
2020/inputs/day12-test1 Normal file
View file

@ -0,0 +1,5 @@
F10
N3
F7
R90
F11