-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay16DragonChecksum.kt
More file actions
29 lines (24 loc) · 1012 Bytes
/
Day16DragonChecksum.kt
File metadata and controls
29 lines (24 loc) · 1012 Bytes
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
package adventofcode.year2016
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.Math.isOdd
class Day16DragonChecksum(
customInput: PuzzleInput? = null,
) : Puzzle(customInput) {
override fun partOne() = input.generateData(272).checksum()
override fun partTwo() = input.generateData(35651584).checksum()
companion object {
private fun String.generateData(length: Int) =
generateSequence(this) { data -> data + '0' + data.reversed().map { char -> if (char == '0') '1' else '0' }.joinToString("") }
.first { data -> data.length >= length }
.take(length)
private fun String.checksum() =
generateSequence(this) { data ->
data
.chunked(2)
.map { char -> if (char.first() == char.last()) '1' else '0' }
.joinToString("")
}.drop(1)
.first { checksum -> checksum.length.isOdd() }
}
}