diff --git a/2020/12/example b/2020/12/example new file mode 100644 index 0000000..d382291 --- /dev/null +++ b/2020/12/example @@ -0,0 +1,5 @@ +F10 +N3 +F7 +R90 +F11 diff --git a/2020/12/input b/2020/12/input new file mode 100644 index 0000000..ab0ccce --- /dev/null +++ b/2020/12/input @@ -0,0 +1,774 @@ +E2 +F24 +F60 +N3 +E1 +R180 +F14 +L90 +N4 +W3 +F81 +R90 +F43 +R90 +F40 +S2 +F56 +L90 +E1 +S1 +R90 +F66 +L180 +W2 +F84 +R90 +E4 +F32 +E5 +R90 +F44 +E1 +R90 +E4 +L90 +N1 +W2 +R90 +E5 +L90 +F86 +L180 +S2 +R180 +E2 +F22 +R180 +F42 +L180 +S4 +W4 +N5 +R270 +F23 +N3 +F18 +E2 +N2 +S3 +W5 +R90 +E4 +S1 +R180 +S1 +F69 +W2 +F55 +E5 +S3 +E4 +N1 +R180 +E1 +S5 +W1 +S5 +L270 +F17 +R180 +N5 +F6 +S3 +R180 +F75 +S3 +F90 +W3 +F98 +N2 +F54 +E5 +S5 +F4 +E5 +R90 +N4 +L90 +E4 +N3 +F75 +E3 +S2 +F89 +E1 +R180 +S3 +E2 +R270 +W4 +L90 +N5 +E4 +W3 +N1 +F37 +L90 +N5 +E3 +N3 +L90 +W1 +R90 +F25 +N2 +R90 +E4 +F81 +N5 +L90 +F47 +E1 +F86 +N5 +W1 +N2 +F35 +L90 +W4 +F35 +R90 +W3 +S3 +W1 +S3 +E5 +L180 +F1 +L90 +E4 +L90 +S4 +F58 +N2 +F95 +L180 +F27 +S4 +L90 +E4 +F90 +E5 +N2 +F36 +W2 +S5 +N3 +W4 +S1 +F58 +E4 +R90 +F60 +E1 +N2 +R90 +N1 +W2 +L90 +N3 +F16 +S2 +R90 +F3 +R270 +F98 +S4 +W1 +S4 +F42 +S5 +N2 +E1 +N4 +E3 +R180 +W3 +R180 +N4 +F47 +N5 +W5 +F55 +E2 +F12 +W3 +N1 +R90 +F55 +R180 +F89 +S4 +F38 +S5 +R180 +N2 +W4 +F16 +N2 +R180 +E2 +R180 +W1 +R90 +F50 +E1 +F36 +W4 +F79 +E3 +F19 +N4 +L270 +N5 +W3 +F48 +R90 +F36 +S5 +F100 +E3 +F55 +N3 +F16 +R180 +S5 +N3 +R180 +N1 +F8 +E5 +L90 +F21 +L90 +F80 +L90 +N5 +W5 +S5 +E5 +R90 +N4 +E1 +F20 +S3 +L90 +E1 +N4 +F16 +F26 +E1 +F75 +E1 +R90 +F69 +N1 +E3 +R90 +F62 +L180 +W4 +F80 +S5 +L90 +F90 +W5 +F40 +W1 +L90 +W1 +R180 +S2 +F65 +E2 +R180 +E1 +F37 +E5 +R90 +N3 +W4 +S5 +E3 +F36 +F8 +W5 +L90 +F54 +N5 +L180 +S4 +L90 +F88 +R180 +F46 +N5 +E3 +F93 +S2 +E5 +R270 +F15 +R90 +F28 +S2 +F61 +N5 +L90 +W3 +S5 +L90 +N1 +L90 +F41 +W4 +N3 +F4 +N3 +F35 +R90 +W5 +F69 +L90 +E3 +R90 +N2 +W4 +N5 +E4 +R90 +N1 +R90 +N2 +F44 +L90 +F15 +W2 +R180 +E3 +L90 +F26 +L90 +S4 +W4 +S1 +F46 +E2 +S5 +R180 +W3 +F6 +L90 +W3 +F99 +E3 +L90 +S1 +E1 +N2 +L90 +N3 +E4 +N3 +W1 +F87 +N2 +L180 +E1 +S1 +E1 +N4 +F20 +N1 +L90 +F41 +N1 +E5 +F20 +N4 +R180 +L180 +F69 +N3 +L90 +F80 +W4 +N5 +F95 +L90 +F27 +N2 +F22 +L270 +F74 +W2 +R180 +S3 +F64 +N3 +L270 +E2 +L90 +E2 +N1 +F72 +W3 +F78 +S3 +F56 +E3 +S4 +R180 +S1 +F53 +E4 +F96 +N1 +L90 +N3 +E5 +F83 +S1 +F26 +S3 +W5 +F20 +R270 +W1 +R90 +N4 +W1 +F69 +R90 +F7 +R180 +F15 +R90 +F65 +S1 +F68 +S5 +R180 +E5 +L90 +S1 +F68 +N1 +F7 +R90 +S1 +W5 +F29 +R270 +E5 +S4 +F14 +W2 +F91 +E2 +S5 +E4 +L90 +F81 +S4 +F69 +R90 +W3 +R180 +F33 +L180 +F52 +S2 +W5 +F2 +N4 +F13 +L270 +F63 +N5 +S3 +W5 +F67 +S1 +L90 +E5 +L90 +S3 +W5 +F67 +R90 +F64 +S4 +E5 +N4 +F97 +E2 +R90 +N1 +F5 +S3 +L90 +S2 +R90 +N3 +E3 +F76 +S4 +R90 +W1 +L180 +W4 +L90 +S3 +L90 +F66 +L90 +E1 +F71 +L270 +F42 +N1 +W1 +L90 +S4 +E3 +F41 +R180 +F68 +W5 +L90 +N5 +E5 +F96 +W3 +F90 +S1 +E1 +R90 +N3 +F38 +R90 +W3 +R90 +W1 +L90 +S5 +F76 +E3 +F36 +S1 +L180 +E1 +F22 +N1 +W1 +R270 +E4 +N5 +E3 +N3 +W2 +L180 +S2 +W5 +N4 +W5 +N3 +R90 +S2 +F45 +R90 +W2 +R90 +W3 +F75 +R180 +E4 +N1 +E4 +L90 +F59 +L90 +S2 +E2 +S2 +R90 +E2 +R90 +F37 +E2 +S1 +S5 +F97 +W4 +N1 +F58 +S2 +F14 +L90 +E3 +R90 +N1 +F4 +F28 +R180 +F55 +N2 +R90 +F48 +E4 +S4 +L90 +N4 +E1 +F67 +S4 +E5 +F16 +E5 +F82 +E4 +L270 +R90 +N5 +L90 +F77 +S3 +E1 +F85 +W3 +S1 +E2 +S5 +R90 +N2 +W5 +E1 +L90 +E3 +R90 +N5 +L270 +N5 +F77 +W4 +N5 +E4 +L90 +E2 +L180 +F13 +E5 +L270 +W1 +F52 +L90 +W3 +R90 +F23 +E1 +F25 +S2 +L180 +F80 +R180 +S5 +E3 +F90 +E4 +F60 +N4 +F75 +E3 +F10 +S3 +E1 +F8 +W5 +R90 +F20 +S3 +R90 +N2 +F41 +L90 +F8 +R270 +W1 +S1 +E2 +L180 +S4 +F52 +E5 +N2 +S3 +W1 +F67 +L90 +E3 +S2 +E2 +F30 +F12 +E3 +F11 +W4 +L90 +S4 +F5 +R90 +S5 +E2 +E3 +R90 +F37 +R90 +F64 +R90 +F68 +L90 +N1 +E2 +N4 +R180 +W4 +L90 +F68 +R90 +N5 +E2 +F54 +W2 +F84 +L90 +E3 +R90 +E2 +F29 +R90 +N1 +E2 +L90 +S5 +F90 +E2 +F11 +S3 +F99 +W3 +S5 +F94 +R90 +F36 diff --git a/2020/12/solution1.py b/2020/12/solution1.py new file mode 100644 index 0000000..98a1e3e --- /dev/null +++ b/2020/12/solution1.py @@ -0,0 +1,44 @@ +import logging +lines = [x.strip() for x in open('input', 'r').readlines()] + +east = 0 +north = 0 +# 0 = E, 1 = N, 2 = W, 3 = S +facing = 0 + + +def serialize(): + return f'N{north} E{east} F{facing}' + + +for line in lines: + pos = serialize() + command = line[0] + number = int(line[1:]) + + if command in ['S', 'W', 'R']: + number *= -1 + elif command in ['F'] and facing in [2, 3]: + number *= -1 + + if command in ['N', 'S']: + north += number + elif command in ['E', 'W']: + east += number + elif command in ['R', 'L']: + facing = (facing + number // 90) % 4 + elif command in ['F']: + if facing % 2 == 0: + east += number + else: + north += number + else: + raise Exception(f'Unknown command: {command}') + + print(f'{line}: \t{pos} -> {serialize()}') + +print() +print(f'{north=} {east=} {facing=}') +manhattan = abs(north) + abs(east) +print('Solution') +print(manhattan) diff --git a/2020/12/solution2.py b/2020/12/solution2.py new file mode 100644 index 0000000..3ea5ee4 --- /dev/null +++ b/2020/12/solution2.py @@ -0,0 +1,48 @@ +lines = [x.strip() for x in open('input', 'r').readlines()] + +east = 0 +north = 0 +# 0 = E, 1 = N, 2 = W, 3 = S + +wp_east = 10 +wp_north = 1 + + +def serialize(): + """Debug a position""" + return f'N{north} E{east} / N{wp_north} E{wp_east}' + + +def rot90(x, y): + """Rotates counter clockwise""" + return [-y, x] + + +for line in lines: + pos = serialize() + command = line[0] + number = int(line[1:]) + + if command in ['S', 'W', 'R']: + number *= -1 + + if command in ['N', 'S']: + wp_north += number + elif command in ['E', 'W']: + wp_east += number + elif command in ['R', 'L']: + number = number // 90 % 4 + for _ in range(number): + wp_east, wp_north = rot90(wp_east, wp_north) + elif command in ['F']: + north += wp_north * number + east += wp_east * number + else: + raise Exception(f'Unknown command: {command}') + + print(f'{line}: \t{pos} -> {serialize()}') + +print() +manhattan = abs(north) + abs(east) +print('Solution') +print(manhattan)