From 9ada5bb7cd0866b2241adb1ef44cc62aca2c4a4c Mon Sep 17 00:00:00 2001 From: Danny Harpigny Date: Fri, 3 Mar 2023 05:22:56 +0100 Subject: [PATCH] --- 2022/10/both.nim | 77 +++++++++++++++++++++++++++++++++++++++++++++++ session.key | Bin 151 -> 151 bytes 2 files changed, 77 insertions(+) create mode 100644 2022/10/both.nim diff --git a/2022/10/both.nim b/2022/10/both.nim new file mode 100644 index 0000000..d42223c --- /dev/null +++ b/2022/10/both.nim @@ -0,0 +1,77 @@ +import + std/strutils + +import + ../../adventofcode + +when not defined(second): + import + std/strformat + +let input = adventofcode.getInput() +let instructions = input.splitLines() + +var registerX = 1 +var currOpIdx = 0 +var currOp = "" +var currOpArg = 0 +var sameOpN = 0 +proc endOp() = + reset currOp + reset currOpArg + reset sameOpN + currOpIdx.inc() + +when not defined(second): + const rangeEnd = 220 + var answer = 0 +else: + const rangeEnd = high(int) + var crtOutput = "" + +for n in 1 .. rangeEnd: + when not defined(second): + if (n + 20) mod 40 == 0: + when not defined(release): + echo fmt"{ n = }, { registerX = }" + answer += n * registerX + else: + # Because the cycle count `n` starts at 1 + let a = n - 1 + + # Only generate a 40x6 image + if a < 40 * 6: + if a mod 40 == 0: + crtOutput.add '\n' + + if a mod 40 in registerX - 1 .. registerX + 1: + crtOutput.add '#' + else: + crtOutput.add '.' + + if currOp == "": + if currOpIdx > high instructions: + break + let opArgs = splitWhitespace instructions[currOpIdx] + currOp = opArgs[0] + if opArgs.len() == 2: + currOpArg = parseInt opArgs[1] + + case currOp + of "addx": + if sameOpN == 1: + registerX += currOpArg + endOp() + of "noop": + endOp() + else: + discard + + if currOp != "": + sameOpN.inc() + + +when not defined(second): + echo fmt"Answer: {answer}" +else: + echo crtOutput diff --git a/session.key b/session.key index dbd46647f2d69398f876c9cf15e673b0b41d21b4..2c9eb747335351662fab252dbc15d5cb9c326aef 100644 GIT binary patch literal 151 zcmV;I0BHXJM@dveQdv+`0CgoHi0a~MiG^@lh?bKvw|@UaXF_er`$jKsQ(je>|DX0C zv5$D~V@&m0FC98dV&KTn3q>v{H0MUJK+Bt*)62ELm6*+Ssua;-X+bPun_eHBXz=?x zQ+^K6)idl%2>Z~oILjJP`l8cofCR)g?dtYZ+@Q< zHM)$^7RGqsTW1f*Hg(qpl2$Zf-tJ9G!)M+&x|)@dw8`~T(oBu`Yyg+&o0J9)94YCu F*<