generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Y2015D16.kt
72 lines (64 loc) · 2.66 KB
/
Y2015D16.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
package aockt.y2015
import io.github.jadarma.aockt.core.Solution
object Y2015D16 : Solution {
private val propertyRegex = Regex("""([a-z]+): (\d+)""")
private val sueIdRegex = Regex("""^Sue (\d+):.*$""")
/** Parses a single line of input and returns what information you know about any [AuntSue]. */
private fun parseInput(input: String) = AuntSue(
id = sueIdRegex.matchEntire(input)!!.groups[1]!!.value.toInt(),
properties = propertyRegex.findAll(input).associate {
val (name, value) = it.destructured
name to value.toInt()
}
)
/** All you know about one of your many aunts. */
private data class AuntSue(val id: Int, val properties: Map<String, Int>)
/**
* Tests these properties against the [mfcsamRules] and returns whether they meet all criteria.
* If there is a rule for a key that is not present in this map, it is ignored.
*/
private fun Map<String, Int>.consistentWith(mfcsamRules: Map<String, (Int) -> Boolean>): Boolean =
mfcsamRules.all { (key, predicate) -> this[key]?.let { value -> predicate(value) } ?: true }
override fun partOne(input: String) =
input
.lineSequence()
.map(this::parseInput)
.first { aunt ->
aunt.properties.consistentWith(
mapOf(
"children" to { it == 3 },
"cats" to { it == 7 },
"samoyeds" to { it == 2 },
"pomeranians" to { it == 3 },
"akitas" to { it == 0 },
"vizslas" to { it == 0 },
"goldfish" to { it == 5 },
"trees" to { it == 3 },
"cars" to { it == 2 },
"perfumes" to { it == 1 },
)
)
}
.id
override fun partTwo(input: String) =
input
.lineSequence()
.map(this::parseInput)
.first { aunt ->
aunt.properties.consistentWith(
mapOf(
"children" to { it == 3 },
"cats" to { it > 7 },
"samoyeds" to { it == 2 },
"pomeranians" to { it < 3 },
"akitas" to { it == 0 },
"vizslas" to { it == 0 },
"goldfish" to { it < 5 },
"trees" to { it > 3 },
"cars" to { it == 2 },
"perfumes" to { it == 1 },
)
)
}
.id
}