aboutsummaryrefslogtreecommitdiff
path: root/2022/nim/day10/src/main.nim
blob: 04b44235bbb689ae9201be0d714241eb7a5a0407 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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