This commit is contained in:
Andrew Lalis 2022-12-10 07:07:46 +01:00
parent aa8a3d5600
commit 4162995fdd
3 changed files with 202 additions and 0 deletions

144
input/10.txt Normal file
View File

@ -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

29
src/s10a.d Normal file
View File

@ -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);
}
}

29
src/s10b.d Normal file
View File

@ -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;
}
}