This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day11.kts
134 lines (117 loc) · 4 KB
/
Day11.kts
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
import java.util.LinkedList
import java.util.Queue
val MONKEY_RE = Regex("Monkey (\\d+):")
val OPERATION_RE = Regex("new = old ([+*]) (.+)")
val DIVISION_RE = Regex("Test: divisible by (\\d+)")
val TRUE_RE = Regex("If true: throw to monkey (\\d+)")
val False_RE = Regex("If false: throw to monkey (\\d+)")
fun parse_operation(operation_spec: Pair<String, String>): (Long) -> Long {
val (op, num) = operation_spec
return if (op == "+") {
if (num == "old") {
{ it + it }
} else {
{ it + num.toInt() }
}
} else if (op == "*") {
if (num == "old") {
{ it * it }
} else {
{ it * num.toInt() }
}
} else {
throw Error("Bad operator: $op")
}
}
class Monkey(
items_: List<Long>,
operation_spec: Pair<String, String>,
public val divisor: Int,
private val true_dest: Int,
private val false_dest: Int,
) {
private val items: Queue<Long> = LinkedList<Long>(items_)
private val operation = parse_operation(operation_spec)
public val n_items: Int
get() = items.size
fun pop_item(): Long = items.remove()
fun catch_item(item: Long) {
items.add(item)
}
fun do_operation(item: Long): Long {
return operation(item)
}
fun get_dest(item: Long): Int {
return if (item.mod(divisor) == 0) {
true_dest
} else {
false_dest
}
}
}
fun parse(data: String): List<Monkey> {
val monkey_blocks = data.split("\n\n")
val to_return = mutableListOf<Monkey>()
var next_i = 0
for (monkey_block in monkey_blocks) {
val monkey_lines = monkey_block.split("\n")
val num = MONKEY_RE.matchEntire(monkey_lines[0].trim())!!.groupValues[1].toInt()
if (num != next_i) {
throw Error("wrong order of monkeys! expected $next_i but got $num")
}
next_i++
val starting_items = monkey_lines[1].split(":")[1].trim().split(", ").map{ it.toLong() }
val operation_spec_ = OPERATION_RE.matchEntire(monkey_lines[2].split(":")[1].trim())!!.groupValues.drop(1)
val operation_spec = Pair(operation_spec_[0], operation_spec_[1])
val divisor = DIVISION_RE.matchEntire(monkey_lines[3].trim())!!.groupValues[1].toInt()
val true_dest = TRUE_RE.matchEntire(monkey_lines[4].trim())!!.groupValues[1].toInt()
val false_dest = False_RE.matchEntire(monkey_lines[5].trim())!!.groupValues[1].toInt()
val monkey = Monkey(
starting_items,
operation_spec,
divisor,
true_dest,
false_dest,
)
to_return.add(monkey)
}
return to_return
}
fun solve(monkeys: List<Monkey>, n_rounds: Int = 20, reduction: (Long) -> Long): Long {
val inspect_counts = Array<Long>(monkeys.size){ 0 }
repeat(n_rounds) {
for ((i, monkey) in monkeys.withIndex()) {
while (monkey.n_items > 0) {
var item = monkey.pop_item()
item = monkey.do_operation(item)
inspect_counts[i]++
item = reduction(item)
val dest = monkey.get_dest(item)
monkeys[dest].catch_item(item)
}
}
}
inspect_counts.sort()
val a = inspect_counts[inspect_counts.size - 2]
val b = inspect_counts[inspect_counts.size - 1]
return a * b
}
fun main() {
val lines = mutableListOf<String>()
while (true) {
val line = readLine()
if (line == null) {
break
}
lines.add(line)
}
val raw = lines.joinToString("\n")
val monkeys1 = parse(raw)
val answer1 = solve(monkeys1, n_rounds = 20, reduction = { it: Long -> it / 3 })
println("part 1: $answer1")
val monkeys2 = parse(raw)
val mod_n = monkeys2.map{ it.divisor }.reduce{ acc, it -> acc * it }.toLong()
val answer2 = solve(monkeys2, n_rounds = 10000, reduction = { it: Long -> it.mod(mod_n) })
println("part 2: $answer2")
}
main()