-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path11.kt
55 lines (48 loc) · 1.78 KB
/
11.kt
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
class Monkey(monkey: String) {
var counter = 0L
var items: MutableList<Long>
var op: (Long) -> Long
val divisibleBy: Long
val idIfTrue: Int
val idIfFalse: Int
companion object {
var modulo: Long = 1
var monkeys: MutableList<Monkey> = mutableListOf()
}
init {
val attributes = monkey.split("\n").map { it.split(":")[1].trim() }
val (_, _, _, opMultOrAdd, opArg) = attributes[2].split(" ")
val opFunc: (Long, Long) -> Long = if (opMultOrAdd == "*") Long::times else Long::plus
items = attributes[1].split(",").map { it.trim().toLong() }.toMutableList()
op = { x -> opFunc(x, opArg.toLongOrNull() ?: x) }
divisibleBy = attributes[3].split(" ").last().toLong()
idIfTrue = attributes[4].split(" ").last().toInt()
idIfFalse = attributes[5].split(" ").last().toInt()
Monkey.modulo *= divisibleBy
Monkey.monkeys.add(this)
}
fun round(divideBy: Int = 1) {
for (item in items) {
val newWorry = op(item) % Monkey.modulo / divideBy
val index = if (newWorry % divisibleBy == 0L) idIfTrue else idIfFalse
Monkey.monkeys[index].items.add(newWorry)
counter++
}
items.clear()
}
}
fun main() {
val monkeys = generateSequence(::readlnOrNull).joinToString("\n").trim().split("\n\n")
fun multLast2() = Monkey.monkeys.map { it.counter }.sorted().takeLast(2).reduce(Long::times)
fun rounds(count: Int, divBy: Int) = repeat(count) { Monkey.monkeys.map { it.round(divBy) } }
// Part 1
monkeys.map { Monkey(it) }
rounds(20, divBy=3)
println(multLast2())
// Part 2
Monkey.modulo = 1
Monkey.monkeys.clear()
monkeys.map { Monkey(it) }
rounds(10000, divBy=1)
println(multLast2())
}