aboutsummaryrefslogtreecommitdiff
path: root/2019/two.nim
blob: c0bfb9bd85da297012c7bc4780cb1290a781ae22 (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
# Day Two: 1202 Program Alarm
import os, strutils, sequtils

let input: string = paramStr(1)
var program: seq[int] = map(split(strip(readFile(input)), ','), parseInt)
program[1] = 12
program[2] = 2

proc execute(program: seq[int]): int =
  var program: seq[int] = program
  var i: int = 0
  while i < len(program):
    case program[i]
    of 1:
      program[program[i+3]] = program[program[i+1]] + program[program[i+2]]
      i += 4
    of 2:
      program[program[i+3]] = program[program[i+1]] * program[program[i+2]]
      i += 4
    of 99:
      break
    else:
      echo("Unsupported code ", program[i], " at ", i)
      quit(QuitFailure)
  return program[0]

proc grammar(program: seq[int]): int =
  var program: seq[int] = program
  for noun in 0 ..< len(program):
    for verb in 0 ..< len(program):
      program[1] = noun
      program[2] = verb
      if execute(program) == 19690720:
        return 100 * noun + verb

echo execute(program)
echo grammar(program)