Created
December 12, 2021 20:46
-
-
Save mchernyavskaya/9c9f7643e0ec60e2de91b9045f161d6c to your computer and use it in GitHub Desktop.
Fish Farm - Advent Of Code - Classes for task 1
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
const val DAYS = 80 | |
const val MORE_DAYS = 256 | |
const val NEWBORN_MAX_SEED = 8 | |
const val MAX_SEED = 6 | |
class Fish(private var seed: Int = NEWBORN_MAX_SEED) { | |
fun plusDay(): Fish? { | |
if (seed == 0) { | |
seed = MAX_SEED | |
return Fish() | |
} | |
seed-- | |
return null | |
} | |
} | |
class FishFarm(input: String) { | |
private val fish = mutableListOf<Fish>() | |
init { | |
fish.addAll(input.split(",") | |
.filter { it.trim().isNotEmpty() } | |
.map { Fish(it.trim().toInt()) } | |
) | |
} | |
private fun plusDay() { | |
val newborns = fish.mapNotNull { it.plusDay() } | |
fish.addAll(newborns) | |
} | |
fun plusDays(days: Int): Int { | |
for (i in 0 until days) { | |
plusDay() | |
} | |
return fish.size | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment