|
| 1 | +package day11 |
| 2 | + |
| 3 | +import locations.Directory.currentDir |
| 4 | +import inputs.Input.loadFileSync |
| 5 | + |
| 6 | +import scala.collection.immutable.Queue |
| 7 | + |
| 8 | +@main def part1: Unit = |
| 9 | + println(s"The solution is ${part1(loadInput())}") |
| 10 | + |
| 11 | +@main def part2: Unit = |
| 12 | + println(s"The solution is ${part2(loadInput())}") |
| 13 | + |
| 14 | +def loadInput(): String = loadFileSync(s"$currentDir/../input/day11") |
| 15 | + |
| 16 | +def part1(input: String): Long = |
| 17 | + run(initial = parseInput(input), times = 20, adjust = _ / 3) |
| 18 | + |
| 19 | +def part2(input: String): Long = |
| 20 | + run(initial = parseInput(input), times = 10_000, adjust = identity) |
| 21 | + |
| 22 | +type Worry = Long |
| 23 | +type Op = Worry => Worry |
| 24 | +type Monkeys = IndexedSeq[Monkey] |
| 25 | + |
| 26 | +case class Monkey( |
| 27 | + items: Queue[Worry], |
| 28 | + divisibleBy: Int, |
| 29 | + ifTrue: Int, |
| 30 | + ifFalse: Int, |
| 31 | + op: Op, |
| 32 | + inspected: Int |
| 33 | +) |
| 34 | + |
| 35 | +def iterate[Z](times: Int)(op: Z => Z)(z: Z): Z = |
| 36 | + (0 until times).foldLeft(z) { (z, _) => op(z) } |
| 37 | + |
| 38 | +def run(initial: Monkeys, times: Int, adjust: Op): Long = |
| 39 | + val lcm = initial.map(_.divisibleBy.toLong).product |
| 40 | + val monkeys = iterate(times)(round(adjust, lcm))(initial) |
| 41 | + monkeys.map(_.inspected.toLong).sorted.reverseIterator.take(2).product |
| 42 | + |
| 43 | +def round(adjust: Op, lcm: Worry)(monkeys: Monkeys): Monkeys = |
| 44 | + monkeys.indices.foldLeft(monkeys) { (monkeys, index) => |
| 45 | + turn(index, monkeys, adjust, lcm) |
| 46 | + } |
| 47 | + |
| 48 | +def turn(index: Int, monkeys: Monkeys, adjust: Op, lcm: Worry): Monkeys = |
| 49 | + val monkey = monkeys(index) |
| 50 | + val Monkey(items, divisibleBy, ifTrue, ifFalse, op, inspected) = monkey |
| 51 | + |
| 52 | + val monkeys1 = items.foldLeft(monkeys) { (monkeys, item) => |
| 53 | + val inspected = op(item) |
| 54 | + val nextWorry = adjust(inspected) % lcm |
| 55 | + val thrownTo = |
| 56 | + if nextWorry % divisibleBy == 0 then ifTrue |
| 57 | + else ifFalse |
| 58 | + val thrownToMonkey = |
| 59 | + val m = monkeys(thrownTo) |
| 60 | + m.copy(items = m.items :+ nextWorry) |
| 61 | + monkeys.updated(thrownTo, thrownToMonkey) |
| 62 | + } |
| 63 | + val monkey1 = monkey.copy( |
| 64 | + items = Queue.empty, |
| 65 | + inspected = inspected + items.size |
| 66 | + ) |
| 67 | + monkeys1.updated(index, monkey1) |
| 68 | +end turn |
| 69 | + |
| 70 | +def parseInput(input: String): Monkeys = |
| 71 | + |
| 72 | + def eval(by: String): Op = |
| 73 | + if by == "old" then identity |
| 74 | + else Function.const(by.toInt) |
| 75 | + |
| 76 | + def parseOperator(op: String, left: Op, right: Op): Op = |
| 77 | + op match |
| 78 | + case "+" => old => left(old) + right(old) |
| 79 | + case "*" => old => left(old) * right(old) |
| 80 | + |
| 81 | + IArray.from( |
| 82 | + for |
| 83 | + case Seq( |
| 84 | + s"Monkey $n:", |
| 85 | + s" Starting items: $items", |
| 86 | + s" Operation: new = $left $operator $right", |
| 87 | + s" Test: divisible by $div", |
| 88 | + s" If true: throw to monkey $ifTrue", |
| 89 | + s" If false: throw to monkey $ifFalse", |
| 90 | + _* |
| 91 | + ) <- input.linesIterator.grouped(7) |
| 92 | + yield |
| 93 | + val op = parseOperator(operator, eval(left), eval(right)) |
| 94 | + val itemsQueue = items.split(", ").map(_.toLong).to(Queue) |
| 95 | + Monkey(itemsQueue, div.toInt, ifTrue.toInt, ifFalse.toInt, op, inspected = 0) |
| 96 | + ) |
| 97 | +end parseInput |
0 commit comments