Created
December 12, 2021 21:07
-
-
Save mchernyavskaya/86e69a1b80558b13f69a473a77bcc9bf to your computer and use it in GitHub Desktop.
Fish Farm Counter - AOC - Day 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FishFarmCounter(input: String) { | |
private val fish = LongArray(NEWBORN_MAX_SEED + 1) | |
init { | |
input.split(",") | |
.filter { it.trim().isNotEmpty() } | |
.map { it.trim().toInt() } | |
.forEach { | |
fish[it]++ | |
} | |
} | |
private fun plusDay() { | |
val parentsCount = fish[0] | |
fish.takeLast(NEWBORN_MAX_SEED) | |
.onEachIndexed { index, count -> | |
fish[index] += count | |
fish[index + 1] -= count | |
} | |
fish[NEWBORN_MAX_SEED] += parentsCount | |
fish[MAX_SEED] += parentsCount | |
fish[0] -= parentsCount | |
} | |
fun plusDays(days: Int): Long { | |
for (i in 0 until days) { | |
plusDay() | |
} | |
return fish.sum() | |
} | |
} | |
// and the calling code | |
fun main() { | |
fun part2(input: List<String>): Long { | |
return FishFarmCounter(input[0]).plusDays(MORE_DAYS) | |
} | |
// test if implementation meets criteria from the description, like: | |
val testInput = readInput("day6/Example_test") | |
val part2 = part2(testInput) | |
check(part2 == 26984457539L) | |
val input = readInput("day6/Example") | |
println("******* PART 2: ${part2(input)} *******") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment