day(06): add solution
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
1f6867dc98
commit
d7cde13a2e
2 changed files with 36 additions and 1 deletions
|
@ -2,12 +2,18 @@ import java.io.File
|
|||
import java.math.BigInteger
|
||||
import java.security.MessageDigest
|
||||
|
||||
private fun openFile(day: Int, name: String) = File("src/day%02d".format(day), "$name.txt")
|
||||
|
||||
/**
|
||||
* Reads lines from the given input txt file.
|
||||
*/
|
||||
fun readInput(day: Int, name: String) = File("src/day%02d".format(day), "$name.txt").readLines()
|
||||
fun readInput(day: Int, name: String) = openFile(day, name).readLines()
|
||||
|
||||
fun readInputAsInts(day: Int, name: String) = readInput(day, name).map { it.toInt() }
|
||||
fun readInputAsCommaSeparatedInts(day: Int, name: String) = openFile(day, name)
|
||||
.readText()
|
||||
.split(",")
|
||||
.map { it.toInt() }
|
||||
|
||||
/**
|
||||
* Converts string to md5 hash.
|
||||
|
|
29
src/day06/Day06.kt
Normal file
29
src/day06/Day06.kt
Normal file
|
@ -0,0 +1,29 @@
|
|||
package day06
|
||||
|
||||
import readInputAsCommaSeparatedInts
|
||||
|
||||
fun howManyAfter(input: List<Int>, days: Int): Long {
|
||||
var counts = List(9) { i -> input.count { it == i }.toLong() }
|
||||
|
||||
for (day in 1..days) {
|
||||
counts = List(9) { i ->
|
||||
when (i) {
|
||||
6 -> counts[0] + counts[7]
|
||||
else -> counts[(i + 1) % 9]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return counts.sum()
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val testInput = readInputAsCommaSeparatedInts(6, "test_input")
|
||||
val input = readInputAsCommaSeparatedInts(6, "input")
|
||||
|
||||
check(howManyAfter(testInput, 80) == 5934.toLong())
|
||||
println(howManyAfter(input, 80))
|
||||
|
||||
check(howManyAfter(testInput, 256) == 26984457539)
|
||||
println(howManyAfter(input, 256))
|
||||
}
|
Loading…
Reference in a new issue