diff --git a/day12/part1/input b/day12/part1/input new file mode 100644 index 0000000..7b923fc --- /dev/null +++ b/day12/part1/input @@ -0,0 +1,774 @@ +E2 +L180 +S4 +R90 +S1 +F49 +N2 +F18 +N2 +L180 +S5 +L90 +E3 +N2 +F11 +L180 +N5 +W3 +L180 +W2 +N5 +F80 +R90 +F89 +N1 +L180 +N2 +R180 +E4 +R90 +S1 +L90 +N5 +R180 +N2 +F17 +L90 +E2 +F58 +W5 +L90 +W3 +N3 +F78 +L90 +N4 +L90 +F15 +W1 +R90 +S1 +W4 +R90 +F41 +W4 +S4 +F37 +E5 +S1 +E3 +F19 +R90 +S1 +W4 +S2 +E2 +L180 +F51 +W5 +R90 +F76 +E2 +F40 +N4 +R180 +E5 +N3 +F72 +S4 +R90 +F99 +E3 +F76 +W5 +R90 +E2 +S5 +R180 +F76 +N4 +L180 +F10 +F83 +S1 +F46 +L90 +S5 +E1 +S1 +F14 +N4 +E1 +R180 +E1 +R180 +S3 +F52 +L90 +S4 +L90 +W3 +F18 +S2 +F81 +L180 +F76 +L180 +W1 +S2 +F73 +N2 +F77 +W1 +F28 +L180 +N2 +F76 +L180 +W5 +F61 +N4 +E2 +R180 +S2 +L90 +F14 +R180 +N5 +E4 +F11 +E1 +L90 +N3 +E3 +F58 +W3 +F72 +W1 +N2 +W5 +F44 +L90 +W4 +F37 +L90 +F4 +W5 +N3 +F57 +N4 +L90 +S2 +F43 +S4 +W3 +S5 +F84 +S2 +L90 +N1 +R180 +W3 +L180 +N2 +W3 +R90 +N2 +R90 +F66 +L90 +F73 +E4 +S2 +L270 +W2 +E2 +S4 +E1 +R90 +W1 +F49 +L270 +F70 +S3 +W1 +N2 +E1 +F65 +W3 +R90 +F27 +E5 +F80 +S4 +W5 +F68 +E5 +R90 +F94 +W2 +L90 +F37 +L180 +E1 +F38 +N2 +F15 +N4 +E3 +L90 +E1 +R90 +S1 +E5 +N4 +N2 +E4 +L270 +N4 +R90 +E1 +S4 +E4 +F71 +E2 +R180 +N5 +E3 +F17 +L90 +N2 +W1 +L90 +F62 +W1 +F85 +W1 +L180 +F33 +S4 +W1 +N5 +F81 +W2 +R90 +S2 +F49 +L180 +S5 +F4 +W4 +N3 +W1 +F17 +R90 +W4 +N4 +E4 +N3 +L90 +S2 +R90 +S3 +L90 +E4 +R90 +S5 +F88 +S1 +L180 +N5 +E5 +F55 +R90 +F81 +E5 +L180 +R90 +F55 +R90 +W5 +F13 +R90 +N5 +F58 +L180 +S5 +F27 +E4 +S3 +F42 +R90 +F39 +W3 +S3 +F31 +S4 +L90 +W1 +S3 +W3 +N4 +W2 +S3 +L90 +F61 +E1 +F23 +S2 +F31 +S3 +L180 +W1 +N1 +L90 +N3 +F81 +E2 +N1 +R90 +F64 +S4 +F88 +E1 +N5 +W1 +S3 +F10 +N5 +L90 +F58 +S1 +R90 +E3 +L90 +N4 +F94 +S1 +W1 +S4 +L90 +F51 +L180 +N4 +R90 +L90 +N4 +F66 +W2 +S3 +S3 +W4 +F68 +L90 +F42 +E1 +F43 +R90 +N3 +F20 +E1 +N3 +E4 +N3 +F4 +S4 +R180 +W1 +R270 +N3 +F86 +L90 +E5 +F84 +N3 +W3 +F16 +L90 +N2 +E3 +L90 +S5 +E5 +F53 +L270 +N2 +F91 +R90 +E5 +N4 +F57 +E5 +S5 +F61 +S4 +F89 +E3 +N3 +N5 +F3 +S5 +F59 +E5 +F66 +R180 +S1 +W1 +N2 +R180 +S4 +E2 +L90 +N1 +W2 +F13 +L90 +E5 +F6 +W3 +F78 +E1 +F7 +W1 +N4 +W5 +F58 +R90 +E4 +N3 +E5 +N3 +W1 +S3 +R90 +F16 +L90 +F93 +R270 +N5 +F2 +W1 +S3 +F54 +R270 +F18 +R180 +F95 +L90 +W1 +E4 +N2 +W1 +L90 +S2 +L90 +W2 +S4 +F92 +W2 +S3 +R180 +N5 +E3 +N5 +E5 +F22 +F88 +S3 +E2 +R90 +S5 +W1 +L90 +E4 +F77 +N1 +W3 +F14 +E3 +R90 +W1 +F21 +N1 +F58 +W4 +N2 +R90 +N2 +W4 +F68 +W5 +N3 +L90 +F22 +R90 +F90 +F84 +S5 +F30 +N1 +W4 +R90 +F17 +R90 +W4 +S5 +E2 +N1 +F92 +N2 +R180 +N5 +E2 +R90 +F38 +R90 +F15 +E5 +N4 +N4 +E4 +S4 +F92 +R90 +F22 +S3 +W4 +N3 +E1 +R180 +F96 +L90 +E1 +N1 +F9 +W2 +N4 +F17 +N2 +R90 +F76 +S2 +F5 +S5 +F34 +R90 +F7 +N4 +F83 +N5 +L90 +W1 +S3 +R90 +S3 +W2 +S3 +F51 +N5 +W4 +F8 +E3 +F10 +N5 +F39 +S3 +E2 +L90 +E5 +L90 +N5 +E2 +N3 +F42 +S3 +F38 +N5 +F19 +F97 +W2 +R180 +S4 +E4 +S2 +W3 +F39 +W4 +F70 +S1 +W1 +R90 +F41 +L90 +E1 +N1 +E3 +W5 +F13 +E4 +F2 +R180 +F27 +E4 +N2 +L270 +E1 +N3 +W4 +F81 +W3 +R90 +E1 +F57 +S5 +R90 +F13 +L180 +N5 +F98 +F32 +N3 +R90 +N3 +W3 +S3 +W3 +N4 +F73 +L180 +N1 +E4 +F7 +E4 +R90 +N4 +R90 +S2 +E5 +F32 +S2 +N5 +W3 +R90 +W5 +S2 +L90 +F4 +R270 +N5 +E3 +L90 +S5 +F24 +N4 +R90 +F27 +L90 +F16 +R90 +N2 +R90 +N3 +L90 +S3 +L90 +F85 +S3 +F47 +N1 +E1 +N3 +R270 +S2 +L90 +F50 +L90 +S2 +F23 +N4 +L180 +E3 +F91 +R90 +E1 +W4 +F81 +W5 +R90 +F46 +E1 +W1 +F91 +N5 +W5 +N3 +W1 +L90 +F60 +S2 +L90 +E1 +F82 +S3 +W5 +N5 +F90 +E3 +S1 +F61 +E4 +F98 +R180 +F8 +R270 +F73 +W4 +L90 +W5 +L90 +F86 +W5 +L180 +F61 +N5 +F88 +E2 +L270 +F90 +N5 +F21 +R270 +F40 +L90 +W1 +N2 +L90 +E2 +S5 +E2 +S1 +E5 +N3 +F51 +S1 +F58 +W3 +L180 +F13 +R90 +N1 +F79 +W2 +F61 +R90 +F22 +E2 +N5 +F1 +S4 +F99 +S1 +S3 +E2 +F97 diff --git a/day12/part1/main.d b/day12/part1/main.d new file mode 100644 index 0000000..d18b7a8 --- /dev/null +++ b/day12/part1/main.d @@ -0,0 +1,89 @@ +import std; + +void main() +{ + slurp!(char, ulong)("input", "%c%d").getManhattanDistanceForInstructions.writeln; +} + +enum Direction +{ + east = 0, + north = 1, + west = 2, + south = 3 +} + +alias Instruction = Tuple!(char, ulong); + +struct ShipState +{ + immutable long posX = 0; + immutable long posY = 0; + immutable Direction direction = Direction.east; + + private auto calculateXMovement(Direction direction, ulong value) + { + switch (direction) with (Direction) + { + case east: + return value; + case west: + return -value; + default: + return 0; + } + } + + private auto calculateYMovement(Direction direction, ulong value) + { + switch (direction) with (Direction) + { + case north: + return value; + case south: + return -value; + default: + return 0; + } + } + + ShipState withInstructionApplied(Instruction instruction) + { + return [ + 'E': (ulong value) => ShipState(posX + value, posY, direction), + 'N': (ulong value) => ShipState(posX, posY + value, direction), + 'W': (ulong value) => ShipState(posX - value, posY, direction), + 'S': (ulong value) => ShipState(posX, posY - value, direction), + 'L': (ulong value) => ShipState(posX, posY, + ((direction + value / 90) % 4).to!Direction), + 'R': (ulong value) => ShipState(posX, posY, + ((direction - value / 90 + 4) % 4).to!Direction), + 'F': (ulong value) => ShipState(posX + calculateXMovement(direction, + value), posY + calculateYMovement(direction, value), direction) + ][instruction[0]](instruction[1]); + } + + ShipState withInstructionsApplied(Instruction[] instructions) + { + if (instructions.empty) + { + return this; + } + return withInstructionApplied(instructions[0]).withInstructionsApplied(instructions[1 .. $]); + } +} + +auto getManhattanDistanceForInstructions(Instruction[] instructions) +{ + auto finalState = ShipState().withInstructionsApplied(instructions); + return abs(finalState.posX) + abs(finalState.posY); +} + +unittest +{ + auto input = [ + tuple('F', 10UL), tuple('N', 3UL), tuple('F', 7UL), tuple('R', 90UL), + tuple('F', 11UL) + ]; + assert(input.getManhattanDistanceForInstructions == 25); +} diff --git a/day12/part2/input b/day12/part2/input new file mode 100644 index 0000000..7b923fc --- /dev/null +++ b/day12/part2/input @@ -0,0 +1,774 @@ +E2 +L180 +S4 +R90 +S1 +F49 +N2 +F18 +N2 +L180 +S5 +L90 +E3 +N2 +F11 +L180 +N5 +W3 +L180 +W2 +N5 +F80 +R90 +F89 +N1 +L180 +N2 +R180 +E4 +R90 +S1 +L90 +N5 +R180 +N2 +F17 +L90 +E2 +F58 +W5 +L90 +W3 +N3 +F78 +L90 +N4 +L90 +F15 +W1 +R90 +S1 +W4 +R90 +F41 +W4 +S4 +F37 +E5 +S1 +E3 +F19 +R90 +S1 +W4 +S2 +E2 +L180 +F51 +W5 +R90 +F76 +E2 +F40 +N4 +R180 +E5 +N3 +F72 +S4 +R90 +F99 +E3 +F76 +W5 +R90 +E2 +S5 +R180 +F76 +N4 +L180 +F10 +F83 +S1 +F46 +L90 +S5 +E1 +S1 +F14 +N4 +E1 +R180 +E1 +R180 +S3 +F52 +L90 +S4 +L90 +W3 +F18 +S2 +F81 +L180 +F76 +L180 +W1 +S2 +F73 +N2 +F77 +W1 +F28 +L180 +N2 +F76 +L180 +W5 +F61 +N4 +E2 +R180 +S2 +L90 +F14 +R180 +N5 +E4 +F11 +E1 +L90 +N3 +E3 +F58 +W3 +F72 +W1 +N2 +W5 +F44 +L90 +W4 +F37 +L90 +F4 +W5 +N3 +F57 +N4 +L90 +S2 +F43 +S4 +W3 +S5 +F84 +S2 +L90 +N1 +R180 +W3 +L180 +N2 +W3 +R90 +N2 +R90 +F66 +L90 +F73 +E4 +S2 +L270 +W2 +E2 +S4 +E1 +R90 +W1 +F49 +L270 +F70 +S3 +W1 +N2 +E1 +F65 +W3 +R90 +F27 +E5 +F80 +S4 +W5 +F68 +E5 +R90 +F94 +W2 +L90 +F37 +L180 +E1 +F38 +N2 +F15 +N4 +E3 +L90 +E1 +R90 +S1 +E5 +N4 +N2 +E4 +L270 +N4 +R90 +E1 +S4 +E4 +F71 +E2 +R180 +N5 +E3 +F17 +L90 +N2 +W1 +L90 +F62 +W1 +F85 +W1 +L180 +F33 +S4 +W1 +N5 +F81 +W2 +R90 +S2 +F49 +L180 +S5 +F4 +W4 +N3 +W1 +F17 +R90 +W4 +N4 +E4 +N3 +L90 +S2 +R90 +S3 +L90 +E4 +R90 +S5 +F88 +S1 +L180 +N5 +E5 +F55 +R90 +F81 +E5 +L180 +R90 +F55 +R90 +W5 +F13 +R90 +N5 +F58 +L180 +S5 +F27 +E4 +S3 +F42 +R90 +F39 +W3 +S3 +F31 +S4 +L90 +W1 +S3 +W3 +N4 +W2 +S3 +L90 +F61 +E1 +F23 +S2 +F31 +S3 +L180 +W1 +N1 +L90 +N3 +F81 +E2 +N1 +R90 +F64 +S4 +F88 +E1 +N5 +W1 +S3 +F10 +N5 +L90 +F58 +S1 +R90 +E3 +L90 +N4 +F94 +S1 +W1 +S4 +L90 +F51 +L180 +N4 +R90 +L90 +N4 +F66 +W2 +S3 +S3 +W4 +F68 +L90 +F42 +E1 +F43 +R90 +N3 +F20 +E1 +N3 +E4 +N3 +F4 +S4 +R180 +W1 +R270 +N3 +F86 +L90 +E5 +F84 +N3 +W3 +F16 +L90 +N2 +E3 +L90 +S5 +E5 +F53 +L270 +N2 +F91 +R90 +E5 +N4 +F57 +E5 +S5 +F61 +S4 +F89 +E3 +N3 +N5 +F3 +S5 +F59 +E5 +F66 +R180 +S1 +W1 +N2 +R180 +S4 +E2 +L90 +N1 +W2 +F13 +L90 +E5 +F6 +W3 +F78 +E1 +F7 +W1 +N4 +W5 +F58 +R90 +E4 +N3 +E5 +N3 +W1 +S3 +R90 +F16 +L90 +F93 +R270 +N5 +F2 +W1 +S3 +F54 +R270 +F18 +R180 +F95 +L90 +W1 +E4 +N2 +W1 +L90 +S2 +L90 +W2 +S4 +F92 +W2 +S3 +R180 +N5 +E3 +N5 +E5 +F22 +F88 +S3 +E2 +R90 +S5 +W1 +L90 +E4 +F77 +N1 +W3 +F14 +E3 +R90 +W1 +F21 +N1 +F58 +W4 +N2 +R90 +N2 +W4 +F68 +W5 +N3 +L90 +F22 +R90 +F90 +F84 +S5 +F30 +N1 +W4 +R90 +F17 +R90 +W4 +S5 +E2 +N1 +F92 +N2 +R180 +N5 +E2 +R90 +F38 +R90 +F15 +E5 +N4 +N4 +E4 +S4 +F92 +R90 +F22 +S3 +W4 +N3 +E1 +R180 +F96 +L90 +E1 +N1 +F9 +W2 +N4 +F17 +N2 +R90 +F76 +S2 +F5 +S5 +F34 +R90 +F7 +N4 +F83 +N5 +L90 +W1 +S3 +R90 +S3 +W2 +S3 +F51 +N5 +W4 +F8 +E3 +F10 +N5 +F39 +S3 +E2 +L90 +E5 +L90 +N5 +E2 +N3 +F42 +S3 +F38 +N5 +F19 +F97 +W2 +R180 +S4 +E4 +S2 +W3 +F39 +W4 +F70 +S1 +W1 +R90 +F41 +L90 +E1 +N1 +E3 +W5 +F13 +E4 +F2 +R180 +F27 +E4 +N2 +L270 +E1 +N3 +W4 +F81 +W3 +R90 +E1 +F57 +S5 +R90 +F13 +L180 +N5 +F98 +F32 +N3 +R90 +N3 +W3 +S3 +W3 +N4 +F73 +L180 +N1 +E4 +F7 +E4 +R90 +N4 +R90 +S2 +E5 +F32 +S2 +N5 +W3 +R90 +W5 +S2 +L90 +F4 +R270 +N5 +E3 +L90 +S5 +F24 +N4 +R90 +F27 +L90 +F16 +R90 +N2 +R90 +N3 +L90 +S3 +L90 +F85 +S3 +F47 +N1 +E1 +N3 +R270 +S2 +L90 +F50 +L90 +S2 +F23 +N4 +L180 +E3 +F91 +R90 +E1 +W4 +F81 +W5 +R90 +F46 +E1 +W1 +F91 +N5 +W5 +N3 +W1 +L90 +F60 +S2 +L90 +E1 +F82 +S3 +W5 +N5 +F90 +E3 +S1 +F61 +E4 +F98 +R180 +F8 +R270 +F73 +W4 +L90 +W5 +L90 +F86 +W5 +L180 +F61 +N5 +F88 +E2 +L270 +F90 +N5 +F21 +R270 +F40 +L90 +W1 +N2 +L90 +E2 +S5 +E2 +S1 +E5 +N3 +F51 +S1 +F58 +W3 +L180 +F13 +R90 +N1 +F79 +W2 +F61 +R90 +F22 +E2 +N5 +F1 +S4 +F99 +S1 +S3 +E2 +F97 diff --git a/day12/part2/main.d b/day12/part2/main.d new file mode 100644 index 0000000..40474ec --- /dev/null +++ b/day12/part2/main.d @@ -0,0 +1,71 @@ +import std; + +void main() +{ + slurp!(char, long)("input", "%c%d").getManhattanDistanceForInstructions.writeln; +} + +alias Instruction = Tuple!(char, long); + +alias RelativeWaypoint = Tuple!(long, long); + +auto rotateLeft(RelativeWaypoint relativeWaypoint, long n) +{ + n = (n + 4) % 4; + if (n == 0) + return relativeWaypoint; + + return tuple(-relativeWaypoint[1], relativeWaypoint[0]).rotateLeft(n - 1); +} + +struct ShipState +{ + immutable long posX = 0; + immutable long posY = 0; + + RelativeWaypoint relativeWaypoint = tuple(10, 1); + + ShipState withInstructionApplied(Instruction instruction) + { + return [ + 'E': (long value) => ShipState(posX, posY, + tuple(relativeWaypoint[0] + value, relativeWaypoint[1])), + 'N': (long value) => ShipState(posX, posY, + tuple(relativeWaypoint[0], relativeWaypoint[1] + value)), + 'W': (long value) => ShipState(posX, posY, + tuple(relativeWaypoint[0] - value, relativeWaypoint[1])), + 'S': (long value) => ShipState(posX, posY, + tuple(relativeWaypoint[0], relativeWaypoint[1] - value)), + 'L': (long value) => ShipState(posX, posY, + relativeWaypoint.rotateLeft(value / 90)), + 'R': (long value) => ShipState(posX, posY, + relativeWaypoint.rotateLeft((360 - value) / 90)), + 'F': (long value) => ShipState(posX + relativeWaypoint[0] * value, + posY + relativeWaypoint[1] * value, relativeWaypoint) + ][instruction[0]](instruction[1]); + } + + ShipState withInstructionsApplied(Instruction[] instructions) + { + if (instructions.empty) + { + return this; + } + return withInstructionApplied(instructions[0]).withInstructionsApplied(instructions[1 .. $]); + } +} + +auto getManhattanDistanceForInstructions(Instruction[] instructions) +{ + auto finalState = ShipState().withInstructionsApplied(instructions); + return abs(finalState.posX) + abs(finalState.posY); +} + +unittest +{ + auto input = [ + tuple('F', 10L), tuple('N', 3L), tuple('F', 7L), tuple('R', 90L), + tuple('F', 11L) + ]; + assert(input.getManhattanDistanceForInstructions == 286); +}