aboutsummaryrefslogtreecommitdiff
path: root/2022/nim/day10
diff options
context:
space:
mode:
Diffstat (limited to '2022/nim/day10')
-rw-r--r--2022/nim/day10/src/main.nim39
1 files changed, 39 insertions, 0 deletions
diff --git a/2022/nim/day10/src/main.nim b/2022/nim/day10/src/main.nim
new file mode 100644
index 0000000..04b4423
--- /dev/null
+++ b/2022/nim/day10/src/main.nim
@@ -0,0 +1,39 @@
+# Day 10: Off-by-One Errors
+import std/[os, strutils, sequtils]
+
+let input = paramStr(1).readFile().strip().split("\n")
+let instructions = input.mapIt((it.split(" ")))
+
+var sum = 0
+var clock = 1
+var register = 1
+
+proc tick() =
+ inc clock
+
+proc write(value: string) =
+ register += value.parseInt()
+
+proc compare() =
+ if clock mod 40 == 20:
+ sum += register * clock
+
+proc render() =
+ if abs(register - ((clock-1) mod 40)) < 2:
+ stdout.write("#")
+ else:
+ stdout.write(".")
+ if clock mod 40 == 0:
+ stdout.write("\n")
+
+for ins in instructions:
+ render()
+ tick()
+ compare()
+ if ins.len == 2:
+ render()
+ tick()
+ write(ins[1])
+ compare()
+
+echo sum