From 7651c291bf36c77d140f56262077971cb60c6247 Mon Sep 17 00:00:00 2001 From: j-james Date: Sun, 11 Dec 2022 15:42:22 -0800 Subject: Day Ten (Nim) --- 2022/nim/day10/src/main.nim | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2022/nim/day10/src/main.nim (limited to '2022/nim') 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 -- cgit v1.2.3-70-g09d2