-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay02.kt
More file actions
44 lines (38 loc) · 1.37 KB
/
Copy pathDay02.kt
File metadata and controls
44 lines (38 loc) · 1.37 KB
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
val gameIdRegex = "Game (\\d+)".toRegex()
val redRegex = "(\\d+) red".toRegex()
val greenRegex = "(\\d+) green".toRegex()
val blueRegex = "(\\d+) blue".toRegex()
class Game(gameString: String) {
val id = gameIdRegex.find(gameString)!!.destructured.component1().toInt()
val blue = blueRegex.findAll(gameString).map {
it.destructured.component1().toInt()
}.toList()
val green = greenRegex.findAll(gameString).map {
it.destructured.component1().toInt()
}.toList()
val red = redRegex.findAll(gameString).map {
it.destructured.component1().toInt()
}.toList()
}
class Day02: BasePuzzle<List<Game>, Int, Int>() {
override fun getPuzzleInput(): List<Game> {
return readLines("day02.txt").filter { it.isNotBlank() }.map { Game(it) }
}
override fun part1(input: List<Game>): Int {
return input.filter {
it.red.max() <= 12 && it.green.max() <= 13 && it.blue.max() <= 14
}.sumOf { it.id }
}
override fun part2(input: List<Game>): Int {
return input.sumOf {
it.red.max() * it.blue.max() * it.green.max()
}
}
companion object {
@JvmStatic fun main(args: Array<String>) {
val dayClass = this::class.java.declaringClass.getDeclaredConstructor()
val day = dayClass.newInstance() as BasePuzzle<*, *, *>
day.main()
}
}
}