-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day4.kt
53 lines (47 loc) · 1.58 KB
/
Day4.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
package aoc2016.day04
import util.readInputLineByLine
fun countValidDoorNumbers(path: String): Int {
var checksumAcc = 0
for (line in readInputLineByLine(path)) {
val split = line.split("[", "]")
val doorId = split[0].substringBeforeLast("-")
val sectorId = split[0].substringAfterLast("-").toInt()
val checksum = split[1]
if (find5MostCommonCharsSorted(doorId) == checksum) {
checksumAcc += sectorId
}
}
return checksumAcc
}
fun decryptAndFindDoor(path: String): Int {
for (line in readInputLineByLine(path)) {
val split = line.split("[", "]")
val doorId = split[0].substringBeforeLast("-")
val sectorId = split[0].substringAfterLast("-").toInt()
if (decrypt(doorId, sectorId) == "northpole object storage") {
return sectorId
}
}
return -1
}
// Group by frequencies, convert to pairs, sort descending by count and then by character
fun find5MostCommonCharsSorted(input: String): String {
val groups = input
.replace("-", "")
.groupBy { it }
.mapValues { it.value.size }
.toList()
.sortedWith(compareBy({ 0 - it.second }, { it.first }))
.take(5)
.map { it.first }
return groups.joinToString("")
}
fun decrypt(input: String, offset: Int = 1): String {
var output = ""
for (char in input) {
output += if (char == '-') ' ' else shiftChar(char, offset)
}
return output
}
private fun shiftChar(char: Char, sectodId: Int): Char =
((((char - 'a') + sectodId) % 26) + 'a'.toInt()).toChar()