aboutsummaryrefslogtreecommitdiff
path: root/2022/nim/day11/src/main.nim
blob: 94781842410a972d8715942e6a079f890a36a42a (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Day 11: Monkey in the Middle
import std/[os, strutils, sequtils, sugar, algorithm]
import npeg # https://github.com/zevv/npeg

let input = paramStr(1).readFile().strip().split("\n\n")

type Monkey = object
  stuff: seq[int]
  inspect: (int) -> int
  throw: (int) -> int
  count: int

var monkey: Monkey
var monkees: seq[Monkey]

let parser = peg(input):
  input <- +(group * "\n\n")
  group <- monkey * "\n" * starting * "\n" * operation * "\n" * test :
    monkees.add(monkey)
  monkey    <- "Monkey " * >Digit * ":":
    monkey = Monkey()
  starting  <- "  Starting items: " * >+Digit * *(", " * >+Digit):
    monkey.stuff = capture[1 ..< capture.len].mapIt(parseInt(it.s))
  operation <- "  Operation: new = old " * >("+" | "*") * " " * >("old" | +Digit):
    let op =
      if $1 == "+":
        (x, y: int) => x + y
      else:
        (x, y: int) => x * y
    if $2 == "old":
      monkey.inspect = (x: int) => x.op(x)
    else:
      monkey.inspect = (x: int) => x.op(parseInt($2))
  test      <- "  Test: divisible by " * >+Digit * "\n" * success * "\n" * failure:
    monkey.throw = (x: int) => (if x mod parseInt($1) == 0: parseInt($2) else: parseInt($3))
  success   <- "    If true: throw to monkey " * >Digit
  failure   <- "    If false: throw to monkey " * >Digit

assert parser.match(paramStr(1).readFile().strip()).ok == true

let lcm = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29][0 ..< monkees.len].foldl(a*b, 1)

var monkeys = monkees
for _ in 0 ..< 20:
  for i, monkey in monkeys:
    while monkeys[i].stuff.len > 0:
      var worry = monkeys[i].stuff.pop()
      monkeys[i].count += 1
      worry = monkeys[i].inspect(worry)
      worry = worry div 3
      monkeys[monkeys[i].throw(worry)].stuff.add(worry)

echo monkeys.mapIt(it.count).sorted(Descending)[0..1].foldl(a*b, 1)

monkeys = monkees
for _ in 0 ..< 10000:
  for i in 0 ..< monkeys.len:
    while monkeys[i].stuff.len > 0:
      var worry = monkeys[i].stuff.pop()
      monkeys[i].count += 1
      worry = monkeys[i].inspect(worry)
      worry = worry mod lcm
      monkeys[monkeys[i].throw(worry)].stuff.add(worry)

echo monkeys.mapIt(it.count).sorted(Descending)[0..1].foldl(a*b, 1)