-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay03NoMatterHowYouSliceIt.kt
More file actions
46 lines (38 loc) · 1.48 KB
/
Day03NoMatterHowYouSliceIt.kt
File metadata and controls
46 lines (38 loc) · 1.48 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
45
46
package adventofcode.year2018
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.cartesianProduct
class Day03NoMatterHowYouSliceIt(
customInput: PuzzleInput? = null,
) : Puzzle(customInput) {
private val claims by lazy { input.lines().map(Claim::invoke) }
private val fabric by lazy { claims.flatMap(Claim::area).groupingBy { it }.eachCount() }
override fun partOne() = fabric.count { it.value > 1 }
override fun partTwo() = claims.first { claim -> claim.area.all { fabric[it] == 1 } }.id
companion object {
private val INPUT_REGEX = """#(\d+) @ (\d+),(\d+): (\d+)x(\d+)""".toRegex()
private data class Claim(
val id: Int,
val left: Int,
val top: Int,
val width: Int,
val height: Int,
) {
val area =
listOf((left until left + width).toList(), (top until top + height).toList())
.cartesianProduct()
.map { it.first() to it.last() }
companion object {
operator fun invoke(input: String): Claim {
val (id, left, top, width, height) =
INPUT_REGEX
.find(input)!!
.destructured
.toList()
.map(String::toInt)
return Claim(id, left, top, width, height)
}
}
}
}
}