aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorj-james2022-12-11 23:42:22 +0000
committerj-james2022-12-12 02:09:22 +0000
commit7651c291bf36c77d140f56262077971cb60c6247 (patch)
tree39be40d9eb749d68be9fb8efcbfc290ab2866c2c
parent3c8ffda3a5e4ffc583e156d91dd1e55065368248 (diff)
Day Ten (Nim)
-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