-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay04SecurityThroughObscurity.kt
More file actions
48 lines (42 loc) · 1.74 KB
/
Day04SecurityThroughObscurity.kt
File metadata and controls
48 lines (42 loc) · 1.74 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
47
48
package adventofcode.year2016
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day04SecurityThroughObscurity(
customInput: PuzzleInput? = null,
) : Puzzle(customInput) {
private val rooms by lazy {
input
.lines()
.map { room -> ROOM_REGEX.find(room)!!.destructured }
.map { (name, sectorId, checksum) -> Triple(name, sectorId.toInt(), checksum) }
}
override fun partOne() =
rooms
.map { (name, sectorId, checksum) -> Triple(name.filterNot { it == '-' }.groupingBy { it }.eachCount(), sectorId, checksum) }
.filter { (letterCount, _, checksum) ->
val computedChecksum =
letterCount
.toList()
.sortedBy { (letter, _) -> letter }
.sortedByDescending { (_, count) -> count }
.take(5)
.joinToString("") { (letter, _) -> letter.toString() }
computedChecksum == checksum
}.sumOf { (_, sectorId, _) -> sectorId }
override fun partTwo() =
rooms
.map { (name, sectorId, _) ->
name
.map { char ->
when (char) {
'-' -> ' '
else -> ((char.code - 'a'.code + sectorId) % 26 + 'a'.code).toChar()
}
}.joinToString("") to sectorId
}.first { (name, _) -> name == NORTHPOLE_OBJECT_STORAGE_ROOM }
.second
companion object {
private val ROOM_REGEX = """([a-z-]+)-(\d+)\[([a-z]{5})]""".toRegex()
private const val NORTHPOLE_OBJECT_STORAGE_ROOM = "northpole object storage"
}
}