generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2016D10.kt
162 lines (135 loc) · 6.68 KB
/
Y2016D10.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package aockt.y2016
import aockt.y2016.Y2016D10.Target.*
import aockt.y2016.Y2016D10.TargetType.*
import io.github.jadarma.aockt.core.Solution
object Y2016D10 : Solution {
/** The possible types of [TargetId]s to receive numbers. */
private enum class TargetType { BOT, OUTPUT }
/** Identifies a target by it's type and identifier. */
private data class TargetId(val type: TargetType, val ordinal: Int)
/** A possible instruction defining part of the simulation. */
private sealed class Instruction(val botId: Int) {
/** An instruction that tells the bot with the given [botId] to receive a new [value]. */
class Take(botId: Int, val value: Int) : Instruction(botId)
/** An instruction that tells the bot with the given [botId] who the targets for the low and high values are. */
class Give(botId: Int, val lowTargetId: TargetId, val highTargetId: TargetId) : Instruction(botId)
companion object {
private val takeRegex = Regex("""value (\d+) goes to bot (\d+)""")
private val giveRegex =
Regex("""bot (\d+) gives low to (bot|output) (\d+) and high to (bot|output) (\d+)""")
/** Returns the [Instruction] described by the [input], throwing [IllegalArgumentException] if invalid. */
fun parse(input: String): Instruction {
takeRegex.matchEntire(input)?.destructured?.let { (value, bot) ->
return Take(bot.toInt(), value.toInt())
}
giveRegex.matchEntire(input)?.destructured?.let { (bot, lowType, lowNumber, highType, highNumber) ->
return Give(
botId = bot.toInt(),
lowTargetId = TargetId(TargetType.valueOf(lowType.uppercase()), lowNumber.toInt()),
highTargetId = TargetId(TargetType.valueOf(highType.uppercase()), highNumber.toInt()),
)
}
throw IllegalArgumentException("Invalid input.")
}
}
}
/** An entity in the simulation that is able to be targeted and given numbers. */
private sealed class Target(val id: TargetId) {
abstract fun receive(value: Int)
/** A target that can hold two values and give them to two other targets. */
class Bot(id: Int) : Target(TargetId(BOT, id)) {
lateinit var lowTargetId: TargetId
lateinit var highTargetId: TargetId
private val values = mutableListOf<Int>()
/** Removes the numbers from this bot's hands and returns them. */
fun takeNumbers(): Pair<Int, Int> {
check(canGive()) { "Cannot take numbers from the bot since both hands are not full." }
return (values[0] to values[1]).also { values.clear() }
}
/** A bot can give its numbers away if it holds one in both hands. */
fun canGive(): Boolean = values.size == 2
/** Puts the [value] in one of the bots' hands, if there is a free one available. */
override fun receive(value: Int) {
check(values.size < 2) { "Cannot receive more values. Bot has full hands." }
values.add(value)
values.sort()
}
}
/** A target that may hold one value, and cannot interact otherwise. */
class Output(id: Int) : Target(TargetId(OUTPUT, id)) {
/** The value currently stored in this output. */
var value: Int = 0
private set
/** Puts the [value] inside this output, replacing any previous value. */
override fun receive(value: Int) {
this.value = value
}
}
companion object {
/** Creates a default Target for the given [targetId]. */
fun createFor(targetId: TargetId): Target = when (targetId.type) {
BOT -> Bot(targetId.ordinal)
OUTPUT -> Output(targetId.ordinal)
}
}
}
/**
* Runs a simulation of inputs, bots, and outputs, and returns the answers to the two problems.
*
* @param instructions A list of instructions that define how the bots behave.
* @param targetNumbers Which pair of numbers to keep an eye on during comparisons.
* The pair should be sorted.
*
* @return A pair of two numbers:
* - The _first_ is the id of the bot that compared the [targetNumbers] together, if any.
* - The _second_ is the product of the numbers stored in outputs 0, 1, and 2.
*/
private fun runSimulation(instructions: List<Instruction>, targetNumbers: Pair<Int, Int>): Pair<Int?, Long> {
require(targetNumbers.first <= targetNumbers.second) { "Target numbers must be sorted." }
val states = mutableMapOf<TargetId, Target>()
instructions.forEach {
val affectedBot = states.getOrPut(TargetId(BOT, it.botId)) { Bot(it.botId) } as Bot
when (it) {
is Instruction.Take -> affectedBot.receive(it.value)
is Instruction.Give -> affectedBot.apply {
states.getOrPut(it.lowTargetId) { Target.createFor(it.lowTargetId) }
states.getOrPut(it.highTargetId) { Target.createFor(it.highTargetId) }
lowTargetId = it.lowTargetId
highTargetId = it.highTargetId
}
}
}
var botThatCompares: Int? = null
var transferComplete = false
while (!transferComplete) {
transferComplete = true
states
.values
.filterIsInstance<Bot>()
.filter { it.canGive() }
.forEach { bot ->
transferComplete = false
val numbers = bot.takeNumbers()
if (numbers == targetNumbers) {
botThatCompares = bot.id.ordinal
}
states.getValue(bot.lowTargetId).receive(numbers.first)
states.getValue(bot.highTargetId).receive(numbers.second)
}
}
val output = listOf(0, 1, 2)
.map { TargetId(OUTPUT, it) }
.map { states.getValue(it) as Output }
.map { it.value.toLong() }
.reduce(Long::times)
return botThatCompares to output
}
override fun partOne(input: String) = runSimulation(
instructions = input.lines().map(Instruction.Companion::parse),
targetNumbers = 17 to 61,
).first!!
override fun partTwo(input: String) = runSimulation(
instructions = input.lines().map(Instruction.Companion::parse),
targetNumbers = 17 to 61,
).second
}