From d9318b5f624e3d6cf7c18ce2aebae2c7766453f9 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sat, 12 Dec 2020 16:28:07 +1030 Subject: [PATCH] 2020 day 12 --- 2020/day12.py | 61 ++++ 2020/input12 | 785 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 846 insertions(+) create mode 100644 2020/day12.py create mode 100644 2020/input12 diff --git a/2020/day12.py b/2020/day12.py new file mode 100644 index 0000000..000f5b8 --- /dev/null +++ b/2020/day12.py @@ -0,0 +1,61 @@ +import numpy as np +with open('input12', 'r') as file: + input = [line.strip() for line in file.readlines()] + +directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]]) + +position = np.array([0,0]) +direction = 0 + +def move_dir(direction, x): + global position, directions + position += x*directions[direction] + +def rotate(degrees): + global direction + direction = (direction + degrees//90)%4 + +moves = { + 'F': lambda x: move_dir(direction, x), + 'E': lambda x: move_dir(0, x), + 'N': lambda x: move_dir(1, x), + 'W': lambda x: move_dir(2, x), + 'S': lambda x: move_dir(3, x), + 'L': lambda x: rotate(x), + 'R': lambda x: rotate(-x), +} + +# position = np.array([0,0]) +for line in input: + moves[line[0]](int(line[1:])) +print(f'Part 1: {position} - manhattan distance = {sum(abs(position))}') + + +waypoint = np.array([10, 1]) +position = np.array([0,0]) + +def move_waypoint(direction, x): + global waypoint + waypoint += x*directions[direction] + +def rotate_waypoint(degrees): + global waypoint + waypoint = np.ravel(waypoint * pow(np.mat([[0, 1],[-1, 0]]), (degrees//90)%4)) + +def move_to_waypoint(x): + global position + position += waypoint * x + +moves_wp = { + 'F': lambda x: move_to_waypoint(x), + 'E': lambda x: move_waypoint(0, x), + 'N': lambda x: move_waypoint(1, x), + 'W': lambda x: move_waypoint(2, x), + 'S': lambda x: move_waypoint(3, x), + 'L': lambda x: rotate_waypoint(x), + 'R': lambda x: rotate_waypoint(-x), +} + +for line in input: + moves_wp[line[0]](int(line[1:])) +print(f'Part 2: {position} - manhattan distance = {sum(abs(position))}') diff --git a/2020/input12 b/2020/input12 new file mode 100644 index 0000000..33221b0 --- /dev/null +++ b/2020/input12 @@ -0,0 +1,785 @@ +N2 +F85 +L90 +W5 +R90 +F56 +F16 +F98 +W4 +S3 +F92 +N3 +W2 +N3 +E2 +S4 +W1 +N2 +F7 +N2 +E3 +S1 +L90 +N2 +E2 +F13 +E5 +S4 +R90 +N2 +W4 +F55 +W3 +N1 +F93 +L90 +N3 +F72 +E4 +W1 +N2 +F76 +S5 +L90 +F44 +N2 +F81 +S1 +F28 +R270 +E5 +N4 +E5 +F23 +R270 +F53 +N2 +L90 +E5 +R180 +N1 +F99 +N3 +E2 +R180 +W5 +F70 +W5 +R180 +F9 +S1 +R90 +S1 +F58 +N3 +N4 +R180 +N3 +R90 +S5 +F6 +E3 +N4 +F38 +W4 +R180 +F53 +R180 +E2 +R90 +W4 +L180 +F13 +N3 +F80 +W1 +R270 +E2 +S1 +F24 +E5 +F1 +S3 +F34 +R90 +F54 +E4 +F59 +E4 +R180 +F88 +R90 +S1 +R90 +F6 +R90 +N1 +F89 +W3 +F9 +L180 +W1 +F47 +R180 +N2 +W1 +F28 +F73 +S1 +F27 +E2 +F82 +W3 +F38 +E4 +L90 +E4 +F64 +W5 +W3 +S2 +L180 +F22 +L90 +F10 +L90 +F64 +E4 +N4 +F21 +S4 +E3 +N5 +F71 +N3 +F93 +N2 +R90 +E4 +F64 +E1 +L90 +F37 +E5 +F96 +W2 +N4 +E2 +R90 +S1 +R90 +W1 +R180 +E2 +S2 +R90 +W3 +S2 +F94 +W2 +L180 +S2 +E4 +F54 +N2 +E4 +S3 +L180 +W2 +N1 +W2 +R90 +F54 +L180 +N2 +F87 +E2 +F23 +W3 +R180 +W4 +S3 +F93 +E5 +R90 +N4 +R90 +S4 +E3 +S3 +F31 +R90 +F89 +E3 +F73 +S5 +F97 +W1 +L180 +F52 +E3 +F68 +R90 +S2 +S5 +F26 +L180 +F12 +E2 +F83 +F9 +N2 +R90 +F99 +R90 +S4 +E5 +R90 +S4 +F84 +W4 +E5 +R90 +F42 +W4 +F94 +W1 +R270 +S3 +E5 +F23 +F38 +W2 +W1 +N5 +E2 +L180 +N1 +F26 +L90 +F5 +R90 +N5 +R180 +F25 +L90 +S1 +N3 +F62 +S1 +F77 +N3 +E3 +L90 +F93 +W3 +E3 +N2 +F14 +N5 +F80 +S2 +L90 +N4 +W2 +L180 +F81 +R180 +N4 +F8 +W4 +S4 +R90 +F34 +W2 +L180 +F49 +S4 +E4 +R90 +W2 +F75 +R90 +S5 +E4 +L90 +N3 +W4 +R90 +N4 +E3 +F62 +R90 +E4 +N2 +W3 +N1 +R90 +F75 +E1 +L180 +S3 +W1 +N2 +W5 +N3 +F70 +L90 +W1 +S2 +L270 +E5 +F21 +S3 +W3 +R90 +F3 +N3 +F85 +E5 +R90 +F8 +W2 +S3 +W4 +F38 +N5 +F54 +L90 +W3 +R90 +E5 +F2 +S5 +F79 +N4 +E2 +R180 +W2 +N1 +R90 +F57 +W4 +S1 +F27 +E5 +F31 +R90 +F11 +S5 +W4 +N3 +W1 +S1 +F93 +N3 +L180 +E1 +F63 +E2 +R180 +F99 +N3 +E3 +N3 +W5 +F66 +E5 +R90 +S1 +L90 +N1 +R90 +W5 +R90 +L90 +E4 +R270 +W3 +N5 +R90 +S5 +F8 +F93 +E3 +N4 +F48 +W1 +R180 +N1 +R90 +W3 +R180 +F3 +R180 +F8 +N4 +E1 +F34 +N1 +F33 +N4 +R90 +L180 +E3 +F35 +S5 +F64 +L90 +E4 +F30 +R90 +L90 +E5 +N4 +R180 +S2 +W3 +R90 +F35 +L180 +E2 +S3 +E5 +L90 +N5 +R90 +R90 +N4 +E5 +F34 +W5 +W4 +L90 +E2 +N2 +S5 +F85 +S5 +F98 +W1 +S3 +F7 +E2 +S4 +F27 +L180 +F30 +L90 +W1 +F87 +W1 +E2 +E2 +N3 +F64 +E4 +F31 +L270 +S3 +W2 +F60 +S2 +E1 +L180 +F2 +W3 +S3 +E3 +R180 +F12 +L180 +E1 +R90 +L180 +F60 +R90 +E4 +R180 +E3 +S1 +E3 +S1 +F86 +R90 +E1 +S4 +F47 +N4 +E4 +N4 +W4 +R90 +S3 +F81 +S1 +E1 +L270 +S2 +L90 +E5 +F62 +W4 +F78 +L90 +F18 +S5 +R180 +S1 +R270 +S3 +W5 +F3 +N1 +R90 +N4 +F59 +N1 +W4 +R90 +E3 +N3 +N4 +L90 +W5 +S2 +L90 +N2 +W1 +L180 +F49 +N4 +F14 +L180 +N2 +F43 +S3 +F84 +E3 +R180 +S4 +W1 +F90 +F58 +W3 +F18 +E5 +L90 +F72 +W1 +N1 +R90 +S3 +F79 +E4 +S4 +F31 +N4 +F66 +L90 +W1 +N2 +E4 +F74 +W1 +N2 +E5 +S5 +L90 +E3 +F71 +N3 +R90 +N4 +E2 +N2 +F83 +L90 +W4 +F64 +L90 +W1 +S2 +E4 +F48 +S3 +W5 +L90 +E5 +S1 +L180 +E4 +N4 +F40 +E4 +R270 +F85 +E4 +R90 +L90 +S1 +R270 +W1 +R180 +F72 +S2 +R90 +F63 +R180 +F93 +E4 +S4 +R90 +W3 +R90 +F10 +E4 +F93 +R180 +E1 +E3 +R90 +W1 +L180 +F83 +W4 +F41 +W4 +R90 +F1 +L90 +W4 +L90 +N4 +R180 +S5 +F63 +S2 +E4 +R90 +F90 +E5 +S2 +L90 +F64 +R90 +L90 +N1 +E4 +F20 +W3 +N4 +R90 +W5 +N1 +E5 +W4 +R90 +N1 +F72 +S5 +F78 +S2 +R180 +N4 +L180 +N5 +F22 +W4 +R180 +F19 +W3 +L90 +E4 +F27 +W5 +S4 +E4 +F34 +N4 +F40 +N3 +R90 +L90 +S2 +W1 +R90 +E3 +R90 +N1 +W4 +F93 +N5 +L90 +F7 +E1 +L90 +W4 +N3 +E5 +F62 +R90 +W4 +L90 +S3 +E4 +L270 +S3 +W5 +S2 +F30 +N2 +F70 +S5 +W1 +S3 +R90 +E4 +S5 +L90 +S2 +R90 +W1 +L90 +W5 +F8 +R90 +N4 +L180 +N5 +L90 +N3 +F30 +W2 +F39 +E3 +F17 +W3 +S1 +F48 +E1 +R90 +F35 +W2 +L180 +S1 +L90 +E5 +S1 +R90 +E2 +S5 +W2 +N1 +E3 +F100 +S2 +E4 +L90 +W5 +R90 +F81 +S4 +R90 +F80