🎄Advent of Code - Day 6 ✨: tidy code
This commit is contained in:
parent
63086e7d7e
commit
bda5097757
1 changed files with 13 additions and 9 deletions
|
@ -11,29 +11,33 @@ class WaitForIt : Puzzle("2023", "6") {
|
||||||
it.split(Regex("\\s")).drop(1).map { it.trim().toLongOrNull() }.filterNotNull()
|
it.split(Regex("\\s")).drop(1).map { it.trim().toLongOrNull() }.filterNotNull()
|
||||||
}.zipWithNext().map {
|
}.zipWithNext().map {
|
||||||
// Take the zipped lines (since we only have 2) and transform them into races.
|
// Take the zipped lines (since we only have 2) and transform them into races.
|
||||||
it.first.mapIndexed { index, i -> Race(i, it.second[index])}
|
it.first.mapIndexed { index, i -> Race(i, it.second[index]) }
|
||||||
}.flatten().also { println("The input is $it") }
|
}.flatten().also {
|
||||||
|
println("The input is $it")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun partOne() {
|
override fun partOne() {
|
||||||
val raceTimes = getRaceTimesPart1().map {
|
getRaceTimesPart1().map {
|
||||||
// here we transform a race into a list of all the possible combinations that we can have
|
// here we transform a race into a list of all the possible combinations that we can have
|
||||||
(1..<it.raceTime).map { time -> Race((it.raceTime - time)* time, it.distance) }
|
(1..<it.raceTime).map { time -> Race((it.raceTime - time) * time, it.distance) }
|
||||||
}.parallelStream().map {
|
}.parallelStream().map {
|
||||||
// Here we filter out the losing races
|
// Here we filter out the losing races
|
||||||
it.filter { it.raceTime > it.distance }.count()
|
it.filter { it.raceTime > it.distance }.count()
|
||||||
}.reduce { acc, i -> acc * i }.get()
|
}.reduce { acc, i -> acc * i }.get().also {
|
||||||
|
println("The number of ways the record was beaten $it.")
|
||||||
println("The number of ways the record was beaten $raceTimes.")
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun partTwo() {
|
override fun partTwo() {
|
||||||
listOf(Race(49787980, 298118510661181)).map {
|
listOf(Race(49787980, 298118510661181)).map {
|
||||||
// here we transform a race into a list of all the possible combinations that we can have
|
// here we transform a race into a list of all the possible combinations that we can have
|
||||||
(1..<it.raceTime).map { time -> Race((it.raceTime - time)* time, it.distance) }
|
(1..<it.raceTime).map { time -> Race((it.raceTime - time) * time, it.distance) }
|
||||||
}.map {
|
}.map {
|
||||||
// Here we filter out the losing races
|
// Here we filter out the losing races
|
||||||
it.parallelStream().filter { it.raceTime > it.distance }.count()
|
it.parallelStream().filter { it.raceTime > it.distance }.count()
|
||||||
}.reduce { acc, i -> acc * i }.also { println("The number of ways the record was beaten $it.") }
|
}.reduce { acc, i -> acc * i }.also {
|
||||||
|
println("The number of ways the record was beaten $it.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue