Skip to content

Commit 345c3b9

Browse files
committed
Solution 2015-16 (Aunt Sue)
1 parent 5108719 commit 345c3b9

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package de.ronny_h.aoc.year2015.day16
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
5+
fun main() = AuntSue().run(373, 260)
6+
7+
class AuntSue : AdventOfCode<Int>(2015, 16) {
8+
private val attributesFromTickerTape = mapOf(
9+
"children" to 3,
10+
"cats" to 7,
11+
"samoyeds" to 2,
12+
"pomeranians" to 3,
13+
"akitas" to 0,
14+
"vizslas" to 0,
15+
"goldfish" to 5,
16+
"trees" to 3,
17+
"cars" to 2,
18+
"perfumes" to 1,
19+
)
20+
21+
override fun part1(input: List<String>): Int = input
22+
.parseSues()
23+
.filter { sue -> sue.attributes.all { attributesFromTickerTape.get(it.key) == it.value } }
24+
.single()
25+
.number
26+
27+
override fun part2(input: List<String>): Int = input
28+
.parseSues()
29+
.filter { sue ->
30+
sue.attributes.all {
31+
when (it.key) {
32+
"cats", "trees" -> attributesFromTickerTape.getValue(it.key) < it.value
33+
"pomeranians", "goldfish" -> attributesFromTickerTape.getValue(it.key) > it.value
34+
else -> attributesFromTickerTape.getValue(it.key) == it.value
35+
}
36+
}
37+
}
38+
.single()
39+
.number
40+
}
41+
42+
fun List<String>.parseSues() = map {
43+
val (number, attributes) = it.substringAfter("Sue ").split(": ", limit = 2)
44+
val attributeMap = attributes.split(", ").map { it.split(": ") }.associate { it[0] to it[1].toInt() }
45+
Sue(number.toInt(), attributeMap)
46+
}
47+
48+
data class Sue(
49+
val number: Int,
50+
val attributes: Map<String, Int>,
51+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package de.ronny_h.aoc.year2015.day16
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.matchers.shouldBe
5+
6+
class AuntSueTest : StringSpec({
7+
8+
val input = listOf(
9+
"Sue 1: cars: 9, akitas: 3, goldfish: 0",
10+
"Sue 2: akitas: 9, children: 3, samoyeds: 9",
11+
"Sue 3: pomeranians: 3, perfumes: 1, vizslas: 0",
12+
"Sue 4: goldfish: 0, vizslas: 0, samoyeds: 2",
13+
"Sue 5: cats: 7, trees: 0, vizslas: 1",
14+
)
15+
16+
"Sues can be parsed" {
17+
input.parseSues() shouldBe listOf(
18+
Sue(1, mapOf("cars" to 9, "akitas" to 3, "goldfish" to 0)),
19+
Sue(2, mapOf("akitas" to 9, "children" to 3, "samoyeds" to 9)),
20+
Sue(3, mapOf("pomeranians" to 3, "perfumes" to 1, "vizslas" to 0)),
21+
Sue(4, mapOf("goldfish" to 0, "vizslas" to 0, "samoyeds" to 2)),
22+
Sue(5, mapOf("cats" to 7, "trees" to 0, "vizslas" to 1)),
23+
)
24+
}
25+
26+
"part 1: The sue that matches all given attributes" {
27+
AuntSue().part1(input) shouldBe 3
28+
}
29+
30+
"part 2: The sue that matches all given attributes respecting the outdated retroencabulator" {
31+
AuntSue().part2(input) shouldBe 4
32+
}
33+
})

0 commit comments

Comments
 (0)