From f956ce3a189272ee212973db01f2aa1aed86b6e4 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Mon, 5 Dec 2022 06:40:55 +0100 Subject: [PATCH] Added day 5. --- input/5.txt | 513 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/s5a.d | 43 +++++ src/s5b.d | 41 +++++ 3 files changed, 597 insertions(+) create mode 100644 input/5.txt create mode 100644 src/s5a.d create mode 100644 src/s5b.d diff --git a/input/5.txt b/input/5.txt new file mode 100644 index 0000000..802f66b --- /dev/null +++ b/input/5.txt @@ -0,0 +1,513 @@ + [M] [B] [N] +[T] [H] [V] [Q] [H] +[Q] [N] [H] [W] [T] [Q] +[V] [P] [F] [Q] [P] [C] [R] +[C] [D] [T] [N] [N] [L] [S] [J] +[D] [V] [W] [R] [M] [G] [R] [N] [D] +[S] [F] [Q] [Q] [F] [F] [F] [Z] [S] +[N] [M] [F] [D] [R] [C] [W] [T] [M] + 1 2 3 4 5 6 7 8 9 + +move 1 from 8 to 7 +move 1 from 2 to 7 +move 6 from 9 to 8 +move 1 from 9 to 1 +move 1 from 9 to 1 +move 3 from 3 to 6 +move 3 from 3 to 9 +move 1 from 9 to 2 +move 5 from 7 to 9 +move 9 from 1 to 6 +move 3 from 4 to 9 +move 2 from 9 to 2 +move 1 from 4 to 2 +move 1 from 3 to 9 +move 8 from 9 to 4 +move 14 from 6 to 7 +move 1 from 3 to 2 +move 5 from 4 to 2 +move 5 from 5 to 7 +move 4 from 2 to 1 +move 2 from 4 to 9 +move 1 from 4 to 3 +move 3 from 5 to 7 +move 1 from 8 to 6 +move 2 from 8 to 7 +move 2 from 1 to 2 +move 1 from 9 to 7 +move 2 from 1 to 3 +move 5 from 6 to 5 +move 4 from 5 to 7 +move 3 from 8 to 4 +move 20 from 7 to 1 +move 11 from 7 to 5 +move 1 from 6 to 9 +move 3 from 9 to 2 +move 12 from 1 to 9 +move 2 from 8 to 3 +move 4 from 2 to 8 +move 8 from 2 to 1 +move 4 from 8 to 9 +move 1 from 2 to 5 +move 12 from 9 to 7 +move 4 from 4 to 9 +move 4 from 9 to 5 +move 13 from 5 to 4 +move 4 from 4 to 7 +move 1 from 7 to 9 +move 2 from 9 to 5 +move 9 from 1 to 2 +move 1 from 8 to 3 +move 5 from 4 to 2 +move 1 from 3 to 6 +move 7 from 2 to 8 +move 6 from 1 to 6 +move 6 from 8 to 7 +move 6 from 2 to 1 +move 3 from 9 to 3 +move 7 from 3 to 7 +move 4 from 4 to 9 +move 1 from 8 to 9 +move 1 from 3 to 9 +move 1 from 2 to 4 +move 1 from 9 to 6 +move 5 from 1 to 9 +move 1 from 4 to 9 +move 2 from 9 to 1 +move 8 from 6 to 7 +move 4 from 9 to 7 +move 2 from 5 to 2 +move 2 from 1 to 9 +move 14 from 7 to 4 +move 22 from 7 to 2 +move 2 from 7 to 4 +move 3 from 7 to 5 +move 9 from 4 to 7 +move 6 from 2 to 4 +move 8 from 4 to 3 +move 14 from 2 to 9 +move 2 from 3 to 9 +move 3 from 2 to 9 +move 4 from 4 to 2 +move 1 from 4 to 5 +move 1 from 1 to 4 +move 5 from 7 to 8 +move 1 from 1 to 3 +move 4 from 5 to 2 +move 6 from 3 to 9 +move 1 from 3 to 4 +move 4 from 8 to 9 +move 2 from 4 to 6 +move 4 from 5 to 3 +move 1 from 7 to 6 +move 1 from 8 to 5 +move 3 from 3 to 1 +move 33 from 9 to 5 +move 5 from 2 to 1 +move 1 from 3 to 5 +move 1 from 7 to 6 +move 18 from 5 to 1 +move 1 from 2 to 8 +move 6 from 5 to 4 +move 1 from 8 to 7 +move 2 from 4 to 1 +move 4 from 1 to 2 +move 19 from 1 to 2 +move 4 from 6 to 8 +move 4 from 1 to 8 +move 14 from 2 to 9 +move 5 from 2 to 4 +move 1 from 8 to 2 +move 8 from 2 to 5 +move 5 from 8 to 4 +move 4 from 9 to 7 +move 1 from 8 to 1 +move 16 from 5 to 4 +move 15 from 4 to 5 +move 1 from 9 to 5 +move 5 from 7 to 6 +move 2 from 7 to 6 +move 1 from 1 to 9 +move 7 from 6 to 7 +move 1 from 8 to 5 +move 1 from 1 to 9 +move 12 from 5 to 7 +move 7 from 5 to 9 +move 12 from 7 to 2 +move 1 from 7 to 4 +move 7 from 4 to 7 +move 2 from 9 to 4 +move 5 from 4 to 9 +move 8 from 2 to 3 +move 4 from 2 to 4 +move 9 from 4 to 8 +move 6 from 3 to 5 +move 8 from 7 to 3 +move 1 from 4 to 3 +move 7 from 8 to 9 +move 4 from 5 to 4 +move 6 from 3 to 1 +move 4 from 3 to 4 +move 1 from 3 to 6 +move 6 from 4 to 9 +move 1 from 6 to 5 +move 17 from 9 to 4 +move 3 from 7 to 3 +move 1 from 7 to 9 +move 2 from 5 to 3 +move 2 from 1 to 3 +move 2 from 8 to 9 +move 1 from 5 to 1 +move 14 from 4 to 5 +move 2 from 3 to 2 +move 1 from 7 to 6 +move 10 from 9 to 4 +move 12 from 9 to 4 +move 9 from 4 to 5 +move 1 from 2 to 9 +move 13 from 5 to 9 +move 2 from 5 to 1 +move 1 from 2 to 9 +move 3 from 4 to 2 +move 12 from 4 to 7 +move 8 from 5 to 7 +move 1 from 1 to 9 +move 1 from 6 to 4 +move 1 from 5 to 4 +move 1 from 4 to 8 +move 5 from 3 to 4 +move 10 from 9 to 6 +move 3 from 6 to 2 +move 7 from 6 to 5 +move 6 from 5 to 4 +move 1 from 8 to 5 +move 1 from 1 to 4 +move 2 from 7 to 2 +move 5 from 4 to 9 +move 2 from 5 to 8 +move 1 from 1 to 3 +move 2 from 1 to 7 +move 6 from 7 to 9 +move 9 from 9 to 8 +move 1 from 1 to 3 +move 4 from 2 to 7 +move 11 from 7 to 3 +move 11 from 8 to 6 +move 7 from 3 to 1 +move 4 from 7 to 2 +move 3 from 2 to 9 +move 8 from 1 to 5 +move 2 from 7 to 5 +move 2 from 2 to 9 +move 2 from 3 to 9 +move 11 from 4 to 7 +move 7 from 9 to 5 +move 6 from 6 to 5 +move 2 from 2 to 9 +move 1 from 2 to 3 +move 6 from 9 to 4 +move 3 from 9 to 1 +move 4 from 3 to 5 +move 6 from 7 to 1 +move 2 from 6 to 3 +move 2 from 9 to 2 +move 3 from 3 to 2 +move 3 from 6 to 8 +move 2 from 7 to 5 +move 20 from 5 to 6 +move 8 from 5 to 1 +move 1 from 5 to 9 +move 2 from 8 to 4 +move 1 from 8 to 7 +move 16 from 1 to 8 +move 8 from 8 to 9 +move 4 from 2 to 4 +move 1 from 1 to 5 +move 1 from 5 to 4 +move 3 from 8 to 4 +move 14 from 4 to 6 +move 5 from 8 to 7 +move 6 from 7 to 8 +move 29 from 6 to 2 +move 3 from 9 to 8 +move 21 from 2 to 3 +move 1 from 8 to 3 +move 6 from 9 to 4 +move 8 from 3 to 5 +move 7 from 8 to 4 +move 7 from 3 to 9 +move 3 from 7 to 2 +move 12 from 4 to 8 +move 2 from 3 to 1 +move 2 from 9 to 1 +move 1 from 6 to 7 +move 1 from 7 to 6 +move 1 from 6 to 3 +move 3 from 1 to 8 +move 2 from 4 to 1 +move 4 from 6 to 1 +move 5 from 2 to 7 +move 1 from 1 to 2 +move 5 from 1 to 2 +move 2 from 8 to 1 +move 1 from 4 to 5 +move 9 from 8 to 4 +move 3 from 7 to 9 +move 7 from 5 to 7 +move 2 from 5 to 9 +move 4 from 9 to 2 +move 3 from 3 to 2 +move 5 from 2 to 7 +move 2 from 8 to 2 +move 2 from 7 to 3 +move 1 from 8 to 6 +move 2 from 1 to 2 +move 1 from 6 to 7 +move 1 from 8 to 1 +move 12 from 7 to 1 +move 5 from 2 to 7 +move 7 from 4 to 2 +move 2 from 4 to 1 +move 5 from 3 to 8 +move 7 from 1 to 9 +move 4 from 7 to 1 +move 7 from 1 to 5 +move 12 from 9 to 2 +move 27 from 2 to 4 +move 3 from 8 to 9 +move 6 from 2 to 5 +move 6 from 1 to 8 +move 1 from 7 to 6 +move 9 from 5 to 2 +move 3 from 9 to 2 +move 13 from 4 to 5 +move 10 from 2 to 7 +move 1 from 9 to 8 +move 11 from 5 to 7 +move 1 from 8 to 7 +move 1 from 2 to 6 +move 13 from 4 to 3 +move 23 from 7 to 4 +move 1 from 6 to 9 +move 1 from 2 to 4 +move 7 from 3 to 5 +move 1 from 9 to 8 +move 19 from 4 to 1 +move 2 from 4 to 1 +move 1 from 7 to 6 +move 1 from 4 to 5 +move 1 from 5 to 7 +move 11 from 5 to 1 +move 2 from 5 to 4 +move 2 from 6 to 9 +move 3 from 8 to 2 +move 2 from 8 to 1 +move 3 from 2 to 1 +move 1 from 9 to 5 +move 6 from 1 to 3 +move 1 from 9 to 7 +move 2 from 7 to 5 +move 2 from 8 to 6 +move 1 from 3 to 2 +move 2 from 8 to 5 +move 1 from 2 to 1 +move 3 from 4 to 1 +move 3 from 5 to 1 +move 2 from 5 to 1 +move 2 from 6 to 9 +move 1 from 9 to 6 +move 1 from 4 to 5 +move 1 from 9 to 8 +move 1 from 8 to 6 +move 8 from 1 to 6 +move 7 from 1 to 8 +move 9 from 1 to 6 +move 1 from 5 to 3 +move 3 from 8 to 4 +move 11 from 3 to 4 +move 1 from 3 to 6 +move 10 from 6 to 8 +move 13 from 1 to 6 +move 3 from 4 to 5 +move 7 from 8 to 6 +move 3 from 8 to 5 +move 6 from 5 to 3 +move 22 from 6 to 9 +move 4 from 3 to 6 +move 4 from 9 to 5 +move 1 from 1 to 5 +move 2 from 3 to 4 +move 2 from 1 to 5 +move 1 from 9 to 2 +move 5 from 8 to 3 +move 2 from 9 to 2 +move 11 from 6 to 9 +move 3 from 2 to 7 +move 1 from 6 to 7 +move 12 from 9 to 8 +move 4 from 7 to 1 +move 12 from 4 to 8 +move 2 from 4 to 7 +move 1 from 1 to 8 +move 1 from 5 to 1 +move 19 from 8 to 4 +move 4 from 5 to 1 +move 1 from 7 to 4 +move 1 from 7 to 1 +move 3 from 3 to 4 +move 2 from 8 to 4 +move 1 from 5 to 7 +move 1 from 7 to 9 +move 8 from 1 to 8 +move 1 from 1 to 4 +move 1 from 3 to 9 +move 1 from 3 to 5 +move 1 from 5 to 2 +move 7 from 8 to 7 +move 16 from 4 to 7 +move 1 from 7 to 4 +move 3 from 8 to 2 +move 14 from 7 to 4 +move 1 from 5 to 8 +move 5 from 7 to 5 +move 16 from 4 to 5 +move 3 from 5 to 4 +move 3 from 2 to 1 +move 1 from 7 to 9 +move 11 from 4 to 2 +move 3 from 8 to 6 +move 2 from 1 to 8 +move 1 from 4 to 9 +move 18 from 5 to 1 +move 1 from 8 to 7 +move 3 from 7 to 9 +move 18 from 9 to 3 +move 3 from 6 to 9 +move 7 from 1 to 6 +move 1 from 8 to 4 +move 1 from 4 to 9 +move 3 from 6 to 4 +move 5 from 9 to 2 +move 2 from 4 to 7 +move 7 from 2 to 8 +move 1 from 7 to 3 +move 2 from 6 to 8 +move 1 from 9 to 5 +move 1 from 6 to 8 +move 1 from 4 to 8 +move 1 from 5 to 3 +move 1 from 7 to 5 +move 8 from 8 to 7 +move 10 from 2 to 6 +move 1 from 9 to 3 +move 6 from 6 to 2 +move 5 from 6 to 2 +move 7 from 2 to 7 +move 12 from 1 to 6 +move 2 from 2 to 1 +move 1 from 2 to 5 +move 4 from 7 to 6 +move 12 from 3 to 1 +move 2 from 7 to 2 +move 9 from 3 to 8 +move 1 from 2 to 6 +move 1 from 5 to 4 +move 9 from 6 to 5 +move 1 from 7 to 6 +move 1 from 4 to 9 +move 9 from 6 to 7 +move 7 from 8 to 3 +move 6 from 3 to 1 +move 4 from 8 to 3 +move 5 from 3 to 1 +move 1 from 9 to 8 +move 2 from 8 to 9 +move 5 from 5 to 7 +move 14 from 7 to 8 +move 1 from 9 to 4 +move 2 from 2 to 1 +move 3 from 5 to 3 +move 2 from 3 to 1 +move 1 from 4 to 6 +move 6 from 8 to 6 +move 6 from 8 to 3 +move 3 from 6 to 1 +move 2 from 8 to 9 +move 19 from 1 to 6 +move 3 from 9 to 3 +move 6 from 3 to 4 +move 6 from 6 to 2 +move 4 from 3 to 9 +move 1 from 7 to 9 +move 2 from 5 to 7 +move 5 from 9 to 6 +move 6 from 7 to 2 +move 11 from 2 to 5 +move 2 from 7 to 4 +move 4 from 4 to 3 +move 2 from 4 to 8 +move 12 from 1 to 2 +move 1 from 8 to 2 +move 8 from 5 to 7 +move 2 from 4 to 9 +move 2 from 7 to 1 +move 4 from 2 to 3 +move 1 from 8 to 6 +move 1 from 1 to 5 +move 2 from 9 to 1 +move 2 from 7 to 3 +move 2 from 5 to 2 +move 1 from 5 to 7 +move 2 from 7 to 8 +move 1 from 5 to 7 +move 5 from 3 to 4 +move 3 from 1 to 7 +move 1 from 2 to 4 +move 15 from 6 to 1 +move 4 from 4 to 1 +move 4 from 2 to 3 +move 8 from 3 to 2 +move 5 from 2 to 4 +move 1 from 8 to 6 +move 1 from 8 to 9 +move 1 from 3 to 1 +move 3 from 7 to 3 +move 5 from 7 to 6 +move 4 from 2 to 9 +move 6 from 2 to 6 +move 4 from 9 to 6 +move 12 from 1 to 5 +move 6 from 4 to 1 +move 1 from 3 to 6 +move 4 from 5 to 8 +move 7 from 5 to 3 +move 3 from 8 to 2 +move 1 from 2 to 3 +move 1 from 9 to 5 +move 1 from 4 to 5 +move 1 from 8 to 5 +move 8 from 6 to 9 +move 10 from 1 to 4 +move 3 from 6 to 1 +move 9 from 3 to 6 +move 1 from 3 to 8 +move 1 from 2 to 4 +move 6 from 9 to 1 +move 1 from 1 to 4 +move 10 from 1 to 6 +move 1 from 8 to 6 +move 13 from 6 to 7 +move 1 from 2 to 1 +move 1 from 9 to 6 +move 9 from 7 to 5 +move 1 from 9 to 4 +move 3 from 7 to 1 +move 3 from 5 to 6 +move 10 from 4 to 7 +move 5 from 6 to 5 +move 3 from 4 to 5 +move 13 from 6 to 9 +move 7 from 5 to 3 +move 6 from 3 to 2 +move 5 from 6 to 4 +move 4 from 2 to 8 diff --git a/src/s5a.d b/src/s5a.d new file mode 100644 index 0000000..4642e73 --- /dev/null +++ b/src/s5a.d @@ -0,0 +1,43 @@ +module s5a; +import util; + +void main() { + string[] fileContent = readText("input/5.txt").splitter("\n\n").array; + string stackContent = fileContent[0]; + char[][] stacks = readStacks(stackContent); + + string instructionContent = fileContent[1].strip; + uint count, origin, dest; + foreach (instruction; instructionContent.splitter("\n")) { + formattedRead(instruction, "move %d from %d to %d", count, origin, dest); + for (int i = 0; i < count; i++) { + stacks[dest-1] ~= stacks[origin - 1][$ - 1]; + stacks[origin-1] = stacks[origin - 1][0 .. $ - 1]; + } + } + writeln(stacks.map!(s => s[$ - 1]).array); +} + +char[][] readStacks(string content) { + char[][] stackArray = content.splitter("\n").array.to!(char[][]); + ulong width = stackArray[0].length; + ulong height = stackArray.length; + char[][] rotatedStackArray; + rotatedStackArray.length = width; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + if (rotatedStackArray[x].length == 0) rotatedStackArray[x].length = height; + rotatedStackArray[x][y] = stackArray[y][x]; + } + } + for (int x = 0; x < rotatedStackArray.length; x++) { + rotatedStackArray[x].reverse; + } + char[][] stacks; + foreach (row; rotatedStackArray) { + if (row.strip.length > 0 && row.strip[0].isNumber) { + stacks ~= row.strip[1..$]; + } + } + return stacks; +} diff --git a/src/s5b.d b/src/s5b.d new file mode 100644 index 0000000..35b59c4 --- /dev/null +++ b/src/s5b.d @@ -0,0 +1,41 @@ +module s5a; +import util; + +void main() { + string[] fileContent = readText("input/5.txt").splitter("\n\n").array; + string stackContent = fileContent[0]; + char[][] stacks = readStacks(stackContent); + + string instructionContent = fileContent[1].strip; + uint count, origin, dest; + foreach (instruction; instructionContent.splitter("\n")) { + formattedRead(instruction, "move %d from %d to %d", count, origin, dest); + stacks[dest - 1] ~= stacks[origin - 1][$ - count .. $]; + stacks[origin - 1] = stacks[origin - 1][0 .. $ - count]; + } + writeln(stacks.map!(s => s[$ - 1]).array); +} + +char[][] readStacks(string content) { + char[][] stackArray = content.splitter("\n").array.to!(char[][]); + ulong width = stackArray[0].length; + ulong height = stackArray.length; + char[][] rotatedStackArray; + rotatedStackArray.length = width; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + if (rotatedStackArray[x].length == 0) rotatedStackArray[x].length = height; + rotatedStackArray[x][y] = stackArray[y][x]; + } + } + for (int x = 0; x < rotatedStackArray.length; x++) { + rotatedStackArray[x].reverse; + } + char[][] stacks; + foreach (row; rotatedStackArray) { + if (row.strip.length > 0 && row.strip[0].isNumber) { + stacks ~= row.strip[1..$]; + } + } + return stacks; +}