/
Day11.scala
142 lines (117 loc) · 4.45 KB
/
Day11.scala
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package nl.grons.advent.y2022
import zio.{Scope, ZIO, ZIOAppArgs, ZIOAppDefault}
import java.nio.file.{Files, Path}
import scala.collection.mutable
/**
* See [[https://adventofcode.com/2022/day/11]].
*/
object Day11 extends ZIOAppDefault {
override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = {
val input = Files.readString(Path.of("src/main/resources/y2022/input-day11.txt"))
case class Operation(lhs: String, op: String, rhs: String) {
assert(lhs.matches("(old|[0-9]+)") && op.matches("[+*]") && rhs.matches("(old|[0-9]+)"))
def apply(input: BigInt): BigInt = {
def value(arg: String) = arg match {
case "old" => input
case intLiteral => BigInt(intLiteral)
}
val lhsValue = value(lhs)
val rhsValue = value(rhs)
op match {
case "+" => lhsValue + rhsValue
case "*" => lhsValue * rhsValue
}
}
}
case class Test(divisor: BigInt, trueMonkey: Int, falseMonkey: Int) {
def targetMonkey(worryLevel: BigInt): Int = if (worryLevel % divisor == 0) trueMonkey else falseMonkey
}
case class Monkey(id: Int, items: Seq[BigInt], operation: Operation, test: Test, inspectCount: BigInt) {
def addItem(worryLevel: BigInt): Monkey = copy(items = items :+ worryLevel)
def doneInspecting: Monkey = copy(items = Seq.empty, inspectCount = inspectCount + items.size)
def keepWorryLevelsManageable(factor: BigInt): Monkey = copy(items = items.map(_ % factor))
}
object Operation {
def fromString(opStr: String): Operation = {
val Array(lhs, op, rhs) = opStr.split(' ')
Operation(lhs, op, rhs)
}
}
object Monkey {
def fromString(lines: Seq[String]): Monkey = {
assert(lines.size == 6)
val id = lines.head.drop(7).dropRight(1).toInt
val items = lines(1).drop(18).split(',').map(s => BigInt(s.trim))
val operationStr = lines(2).drop(19)
val divisor = lines(3).drop(21).toInt
val trueMonkey = lines(4).drop(29).toInt
val falseMonkey = lines(5).drop(30).toInt
Monkey(id, items, Operation.fromString(operationStr), Test(divisor, trueMonkey, falseMonkey), 0)
}
}
val inputMonkeys: Seq[Monkey] = input
.linesIterator
.sliding(6, 7)
.map(Monkey.fromString)
.toIndexedSeq
val resultPart1 = {
val monkeys: mutable.Seq[Monkey] = inputMonkeys.to(mutable.Seq)
def turn(monkeyId: Int): Unit = {
val monkey = monkeys(monkeyId)
monkey.items.foreach { item =>
val worryLevel = monkey.operation(item) / 3
val targetMonkeyId = monkey.test.targetMonkey(worryLevel)
assert(targetMonkeyId != monkeyId)
val targetMonkey = monkeys(targetMonkeyId)
monkeys.update(targetMonkeyId, targetMonkey.addItem(worryLevel))
}
monkeys.update(monkeyId, monkey.doneInspecting)
}
def round(): Unit = {
monkeys.indices.foreach(turn)
}
20.times(round())
val levelOfMonkeyBusiness =
monkeys
.map(_.inspectCount)
.sorted
.takeRight(2)
.product
levelOfMonkeyBusiness
}
// Part 2
val resultPart2 = {
val monkeys: mutable.Seq[Monkey] = inputMonkeys.to(mutable.Seq)
val factor = monkeys.map(_.test.divisor).product
def turn(monkeyId: Int): Unit = {
val monkey = monkeys(monkeyId)
monkey.items.foreach { item =>
val worryLevel = monkey.operation(item)
val targetMonkeyId = monkey.test.targetMonkey(worryLevel)
assert(targetMonkeyId != monkeyId)
val targetMonkey = monkeys(targetMonkeyId)
monkeys.update(targetMonkeyId, targetMonkey.addItem(worryLevel))
}
monkeys.update(monkeyId, monkey.doneInspecting)
}
def keepWorryLevelsManageble(monkeyId: Int): Unit = {
val monkey = monkeys(monkeyId)
monkeys.update(monkeyId, monkey.keepWorryLevelsManageable(factor))
}
def round(): Unit = {
monkeys.indices.foreach(turn)
monkeys.indices.foreach(keepWorryLevelsManageble)
}
10000.times(round())
val levelOfMonkeyBusiness =
monkeys
.map(_.inspectCount)
.sorted
.takeRight(2)
.product
levelOfMonkeyBusiness
}
zio.Console.printLine("Result part 1: " + resultPart1) *> // 120384
zio.Console.printLine("Result part 2: " + resultPart2) // 32059801242
}
}