Skip to content

Commit c2cb23d

Browse files
committed
Solution 2017-08 (I Heard You Like Registers)
1 parent 431ca95 commit c2cb23d

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package de.ronny_h.aoc.year2017.day08
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
5+
fun main() = IHeardYouLikeRegisters().run(8022, 9819)
6+
7+
class IHeardYouLikeRegisters : AdventOfCode<Int>(2017, 8) {
8+
override fun part1(input: List<String>): Int = executeInstructions(input).maxFinalValue
9+
override fun part2(input: List<String>): Int = executeInstructions(input).maxProcessValue
10+
11+
data class Result(val maxFinalValue: Int, val maxProcessValue: Int)
12+
13+
private fun executeInstructions(input: List<String>): Result {
14+
val registers = mutableMapOf<String, Int>()
15+
var maxProcessValue = 0
16+
input.parseInstructions().forEach {
17+
if (it.condition.appliesTo(registers.getOrDefault(it.conditionRegister, 0), it.conditionValue)) {
18+
val value = it.operation.applyTo(registers.getOrDefault(it.register, 0), it.amount)
19+
if (value > maxProcessValue) {
20+
maxProcessValue = value
21+
}
22+
registers[it.register] = value
23+
}
24+
}
25+
return Result(registers.values.max(), maxProcessValue)
26+
}
27+
}
28+
29+
enum class Operation(private val symbol: String) {
30+
Increase("inc") {
31+
override fun applyTo(first: Int, second: Int): Int = first + second
32+
},
33+
Decrease("dec") {
34+
override fun applyTo(first: Int, second: Int): Int = first - second
35+
};
36+
37+
abstract fun applyTo(first: Int, second: Int): Int
38+
39+
companion object {
40+
private val bySymbol = Operation.entries.associateBy { it.symbol }
41+
42+
fun of(symbol: String) = bySymbol.getValue(symbol)
43+
}
44+
}
45+
46+
enum class Condition(private val symbol: String) {
47+
Equal("==") {
48+
override fun appliesTo(first: Int, second: Int): Boolean = first == second
49+
},
50+
NotEqual("!=") {
51+
override fun appliesTo(first: Int, second: Int): Boolean = first != second
52+
},
53+
LessThan("<") {
54+
override fun appliesTo(first: Int, second: Int): Boolean = first < second
55+
},
56+
LessThanOrEqual("<=") {
57+
override fun appliesTo(first: Int, second: Int): Boolean = first <= second
58+
},
59+
GreaterThan(">") {
60+
override fun appliesTo(first: Int, second: Int): Boolean = first > second
61+
},
62+
GreaterThanOrEqual(">=") {
63+
override fun appliesTo(first: Int, second: Int): Boolean = first >= second
64+
};
65+
66+
abstract fun appliesTo(first: Int, second: Int): Boolean
67+
68+
companion object {
69+
private val bySymbol = Condition.entries.associateBy { it.symbol }
70+
71+
fun of(symbol: String) = bySymbol.getValue(symbol)
72+
}
73+
}
74+
75+
data class Instruction(
76+
val register: String,
77+
val operation: Operation,
78+
val amount: Int,
79+
val conditionRegister: String,
80+
val condition: Condition,
81+
val conditionValue: Int
82+
)
83+
84+
fun List<String>.parseInstructions(): List<Instruction> = map {
85+
val parameters = it.split(" ")
86+
Instruction(
87+
register = parameters[0],
88+
operation = Operation.of(parameters[1]),
89+
amount = parameters[2].toInt(),
90+
conditionRegister = parameters[4],
91+
condition = Condition.of(parameters[5]),
92+
conditionValue = parameters[6].toInt(),
93+
)
94+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package de.ronny_h.aoc.year2017.day08
2+
3+
import de.ronny_h.aoc.extensions.asList
4+
import de.ronny_h.aoc.year2017.day08.Condition.*
5+
import de.ronny_h.aoc.year2017.day08.Operation.Decrease
6+
import de.ronny_h.aoc.year2017.day08.Operation.Increase
7+
import io.kotest.core.spec.style.StringSpec
8+
import io.kotest.matchers.shouldBe
9+
10+
class IHeardYouLikeRegistersTest : StringSpec({
11+
12+
val input = """
13+
b inc 5 if a > 1
14+
a inc 1 if b < 5
15+
c dec -10 if a >= 1
16+
c inc -20 if c == 10
17+
""".asList()
18+
19+
"input can be parsed" {
20+
input.parseInstructions() shouldBe listOf(
21+
Instruction("b", Increase, 5, "a", GreaterThan, 1),
22+
Instruction("a", Increase, 1, "b", LessThan, 5),
23+
Instruction("c", Decrease, -10, "a", GreaterThanOrEqual, 1),
24+
Instruction("c", Increase, -20, "c", Equal, 10),
25+
)
26+
}
27+
28+
"part 1: the largest value in any register after completing the instructions" {
29+
IHeardYouLikeRegisters().part1(input) shouldBe 1
30+
}
31+
32+
"part 2: the highest value held in any register during this process" {
33+
IHeardYouLikeRegisters().part2(input) shouldBe 10
34+
}
35+
})

0 commit comments

Comments
 (0)