easy peasy

This commit is contained in:
a 2020-12-12 01:11:00 -06:00
parent 9cb41474df
commit c6b5153768
3 changed files with 913 additions and 0 deletions

137
2020/12/12.py Normal file
View file

@ -0,0 +1,137 @@
def read_file():
with open("input.txt","r") as f:
return f.read().split('\n')
def navigate(instructions):
direction = "E"
x = 0
y = 0
for instruction in instructions:
action = instruction[0]
value = int(instruction[1:])
if action == "N":
y += value
elif action == "S":
y -= value
elif action == "E":
x += value
elif action == "W":
x -= value
elif action == "F":
if direction == "N":
y += value
elif direction == "S":
y -= value
elif direction == "E":
x += value
elif direction == "W":
x -= value
elif action == "L":
if direction == "E":
if value == 90:
direction = "N"
elif value == 180:
direction = "W"
elif value == 270:
direction = "S"
elif direction == "N":
if value == 90:
direction = "W"
elif value == 180:
direction = "S"
elif value == 270:
direction = "E"
elif direction == "W":
if value == 90:
direction = "S"
elif value == 180:
direction = "E"
elif value == 270:
direction = "N"
elif direction == "S":
if value == 90:
direction = "E"
elif value == 180:
direction = "N"
elif value == 270:
direction = "W"
elif action == "R":
if direction == "E":
if value == 90:
direction = "S"
elif value == 180:
direction = "W"
elif value == 270:
direction = "N"
elif direction == "S":
if value == 90:
direction = "W"
elif value == 180:
direction = "N"
elif value == 270:
direction = "E"
elif direction == "W":
if value == 90:
direction = "N"
elif value == 180:
direction = "E"
elif value == 270:
direction = "S"
elif direction == "N":
if value == 90:
direction = "E"
elif value == 180:
direction = "S"
elif value == 270:
direction = "W"
return x, y, direction
def navigate2(instructions):
x = 0
y = 0
wx = 10
wy = 1
for instruction in instructions:
action = instruction[0]
value = int(instruction[1:])
if action == "N":
wy += value
elif action == "S":
wy -= value
elif action == "E":
wx += value
elif action == "W":
wx -= value
elif action == "F":
x += value * wx
y += value * wy
elif action == "L":
if value == 90:
wx, wy = -wy, wx
elif value == 180:
wx, wy = -wx, -wy
elif value == 270:
wx, wy = wy, -wx
elif action == "R":
if value == 90:
wx, wy = wy, -wx
elif value == 180:
wx, wy = -wx, -wy
elif value == 270:
wx, wy = -wy, wx
return x, y
def part1(instructions):
x, y, direction = navigate(instructions)
return abs(x) + abs(y)
def part2(instructions):
x, y = navigate2(instructions)
return abs(x) + abs(y)
def main():
instructions = read_file()
print(part1(instructions), part2(instructions))
if __name__ == "__main__":
main()

771
2020/12/input.txt Normal file
View file

@ -0,0 +1,771 @@
F92
R180
S1
F64
R90
S1
E1
F11
N4
R180
S3
E3
F55
R90
N1
E4
L180
F9
N3
R90
W4
N4
F36
L90
F50
E3
F9
E5
S1
W2
E4
R180
N3
F18
N2
S5
W1
F41
E3
N4
F66
R270
F22
N5
E4
N1
L90
W1
N1
R90
F87
E1
N4
E1
L90
E5
L180
F84
S5
F65
N2
W1
F65
N3
L90
R90
E1
L180
S1
F60
E3
L90
F22
E5
F32
S1
L90
E2
S2
W5
N2
E5
F60
L180
W3
F49
S5
F84
N5
F78
W5
S3
L90
S3
F83
L90
E1
L90
E1
F99
W1
L90
W2
L90
S1
R180
F8
E2
R180
N2
F66
N5
L90
E1
F76
W3
F23
F79
N3
E1
R90
N5
F33
N2
L180
F97
R90
L90
S5
F99
W5
L90
F47
L90
E4
F99
S4
R90
E2
S2
L90
F33
E2
F71
R90
N1
N2
W3
L90
W4
S1
R90
F27
L270
W5
F49
W3
F41
L90
F23
L90
S1
F11
L90
W3
N1
W4
L90
N4
W2
L90
S3
W4
L180
W4
R180
W4
F49
E3
S2
F64
S4
F61
E3
L180
W1
N2
W2
F64
R90
S4
E5
N1
L270
E3
F22
E2
R90
S5
F57
N2
F78
L270
W1
N4
L90
S4
F71
S2
L90
E1
L180
F4
N1
W4
L90
N5
F98
N3
L90
W2
L180
F54
W1
R90
W3
F67
L90
W2
R180
F79
N4
F30
L90
N4
R180
L90
S5
F7
N2
F26
S5
F43
E3
N3
L180
F57
W4
L90
E5
F100
S2
F86
L180
W3
S5
W1
S3
L180
S2
S5
W4
L90
F73
R90
F14
E2
F99
R90
R90
E5
N1
R90
N1
N4
R180
S4
L90
F98
W2
L90
F100
R180
W4
F92
E1
L180
S3
F94
E4
N5
R90
S3
W4
F86
S5
R90
E2
F6
E2
F75
N1
L90
W4
F2
E1
N4
E1
L90
W4
F17
N2
R90
E5
R90
N1
W1
R180
W4
R180
F97
E5
N2
L90
F27
N1
W5
R90
N1
F98
R90
N2
F12
W5
S1
F86
N3
W3
F71
L180
W1
R90
W3
S4
E2
S5
W3
R90
E4
R90
W1
N3
F11
W4
E4
F67
E2
F15
W5
N4
W1
R90
L90
W4
F16
N4
F87
L180
F73
L180
E5
R90
F96
N5
W1
F28
R90
W5
S3
S3
E5
R180
E4
F4
L90
E3
E4
S5
W3
L180
F57
R90
N2
F78
N2
L90
S1
R90
W4
S1
L90
N5
F60
R90
S4
F42
R90
W2
F31
R180
N2
S2
F91
S4
R90
N2
L180
N1
E4
F72
E3
E3
N2
L90
N1
L90
F83
S1
R90
N5
E4
F53
N3
E5
S3
E4
F93
W2
F53
L90
E2
L90
W2
F96
R90
E3
L90
S5
F31
R90
R90
F98
F91
W2
N3
F69
R90
F51
R90
N1
E2
R90
S1
R180
E5
N5
W5
F34
W1
F60
R90
F53
R90
F62
R90
S4
F95
W5
E2
F84
R90
E2
F54
E2
F5
R90
F32
L90
E2
S2
L180
F31
E2
L90
N5
W2
L90
F53
R90
S1
R90
F49
R90
S3
L90
F76
F31
S1
F13
R90
W1
S1
R90
W2
L180
E2
R90
E2
N1
W1
S1
E1
S4
L90
N2
E2
R90
S4
L90
N5
L90
W4
S2
W1
L90
E4
R90
W2
S1
L90
N5
F63
R90
W1
F20
L90
F86
S4
S4
E2
F92
F90
W4
F94
E1
N5
F30
W3
N3
L180
W5
F52
R270
E5
R90
N4
W4
F10
N1
F16
N1
F31
S4
L90
F81
N1
L90
W2
F84
L90
F79
L90
W5
L90
F90
R90
E2
L90
F25
N5
S1
F12
R90
F34
R90
F4
E4
L90
S1
F70
R90
F22
R90
F67
R90
F40
W1
S3
L180
N2
E4
S4
F98
R180
F16
S2
W4
S1
F70
L90
N3
E3
L270
W4
L90
W5
R180
W5
N4
R90
W4
F5
L270
E3
L90
E5
F50
S3
F51
E1
S5
F95
R90
L90
S2
F75
R90
W2
F70
E2
F52
W4
E1
L180
F72
N2
F5
E2
F94
R180
F7
E3
N2
R90
W1
N4
R90
S1
W4
L270
W1
W1
N2
E1
F31
N1
W4
S5
L90
E1
R180
F50
N1
W2
F66
N2
L90
R90
W1
E5
L90
E2
R180
F63
N4
N3
F87
W4
L90
F85
N3
F93
S2
F95
N5
L180
S3
R90
W5
L180
W1
L90
S4
F68
L180
S4
R180
E2
L180
E3
R180
S5
F88
N1
R90
S3
E5
S5
E1
N5
E2
S1
W5
F62
R90
F39
L180
N3
F42
E4
N5
R90
W5
S2
F43
W4
F94
R90
F85
N5
F7
L180
W4
L180
E4
F44
N2
W2
N3
L180
E3
N1
S2
W4
N5
R180
F78
E5
L90
W4
F77

5
2020/12/input_sample.txt Normal file
View file

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