From 4162995fdd7561661d2bb102daea761924db70fb Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sat, 10 Dec 2022 07:07:46 +0100 Subject: [PATCH] Day 10 --- input/10.txt | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/s10a.d | 29 +++++++++++ src/s10b.d | 29 +++++++++++ 3 files changed, 202 insertions(+) create mode 100644 input/10.txt create mode 100644 src/s10a.d create mode 100644 src/s10b.d diff --git a/input/10.txt b/input/10.txt new file mode 100644 index 0000000..eb4a645 --- /dev/null +++ b/input/10.txt @@ -0,0 +1,144 @@ +addx 2 +addx 3 +addx 1 +noop +addx 4 +noop +noop +noop +addx 5 +noop +addx 1 +addx 4 +addx -2 +addx 3 +addx 5 +addx -1 +addx 5 +addx 3 +addx -2 +addx 4 +noop +noop +noop +addx -27 +addx -5 +addx 2 +addx -7 +addx 3 +addx 7 +addx 5 +addx 2 +addx 5 +noop +noop +addx -2 +noop +addx 3 +addx 2 +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx -29 +addx 30 +addx -26 +addx -10 +noop +addx 5 +noop +addx 18 +addx -13 +noop +noop +addx 5 +noop +noop +addx 5 +noop +noop +noop +addx 1 +addx 2 +addx 7 +noop +noop +addx 3 +noop +addx 2 +addx 3 +noop +addx -37 +noop +addx 16 +addx -12 +addx 29 +addx -16 +addx -10 +addx 5 +addx 2 +addx -11 +addx 11 +addx 3 +addx 5 +addx 2 +addx 2 +addx -1 +addx 2 +addx 5 +addx 2 +noop +noop +noop +addx -37 +noop +addx 17 +addx -10 +addx -2 +noop +addx 7 +addx 3 +noop +addx 2 +addx -10 +addx 22 +addx -9 +addx 5 +addx 2 +addx -5 +addx 6 +addx 2 +addx 5 +addx 2 +addx -28 +addx -7 +noop +noop +addx 1 +addx 4 +addx 17 +addx -12 +noop +noop +noop +noop +addx 5 +addx 6 +noop +addx -1 +addx -17 +addx 18 +noop +addx 5 +noop +noop +noop +addx 5 +addx 4 +addx -2 +noop +noop +noop +noop +noop diff --git a/src/s10a.d b/src/s10a.d new file mode 100644 index 0000000..e86aac1 --- /dev/null +++ b/src/s10a.d @@ -0,0 +1,29 @@ +module s10a; +import util; + +void main() { + string[] commands = readText("input/10.txt").strip.splitter("\n").array; + int x = 1; + int sum = 0; + ulong cycleCount = 0; + size_t commandIdx = 0; + while (commandIdx < commands.length) { + string command = commands[commandIdx++]; + checkSignalStrength(cycleCount + 1, x, sum); + cycleCount++; + if (command.startsWith("addx")) { + checkSignalStrength(cycleCount + 1, x, sum); + x += command[5..$].to!int; + cycleCount++; + } + } + writeln(sum); +} + +void checkSignalStrength(ulong i, int x, ref int sum) { + if (i == 20 || i == 60 || i == 100 || i == 140 || i == 180 || i == 220) { + int signalStrength = cast(int) i * x; + sum += signalStrength; + writefln!"During cycle %d, x = %d, signal strength: %d"(i, x, signalStrength); + } +} diff --git a/src/s10b.d b/src/s10b.d new file mode 100644 index 0000000..7bb77f0 --- /dev/null +++ b/src/s10b.d @@ -0,0 +1,29 @@ +module s10a; +import util; + +void main() { + string[] commands = readText("input/10.txt").strip.splitter("\n").array; + int x = 1; + size_t commandIdx = 0; + size_t crtIdx = 0; + char[] crtRow; + while (commandIdx < commands.length) { + string command = commands[commandIdx++]; + drawCrt(crtIdx, crtRow, x); + if (command.startsWith("addx")) { + drawCrt(crtIdx, crtRow, x); + x += command[5..$].to!int; + } + } +} + +void drawCrt(ref size_t crtIdx, ref char[] crtRow, int x) { + bool lit = x == crtIdx - 1 || x == crtIdx || x == crtIdx + 1; + crtRow ~= lit ? '#' : '.'; + crtIdx++; + if (crtIdx > 39) { + crtIdx = 0; + writeln(crtRow); + crtRow.length = 0; + } +}