diff --git a/src/main/kotlin/AdventOfCode2023/day6/WaitForIt.kt b/src/main/kotlin/AdventOfCode2023/day6/WaitForIt.kt index 871e58c..ecc0fe5 100644 --- a/src/main/kotlin/AdventOfCode2023/day6/WaitForIt.kt +++ b/src/main/kotlin/AdventOfCode2023/day6/WaitForIt.kt @@ -5,37 +5,28 @@ import AdventOfCode.Puzzle data class Race(val raceTime: Long, val distance: Long) class WaitForIt : Puzzle("2023", "6") { - fun getRaceTimesPart1(): List { - return inputData.map { - // Split on spaces, ignore first item, transform tokens to integers - it.split(Regex("\\s")).drop(1).map { it.trim().toLongOrNull() }.filterNotNull() - }.zipWithNext().map { - // Take the zipped lines (since we only have 2) and transform them into races. - it.first.mapIndexed { index, i -> Race(i, it.second[index]) } - }.flatten().also { - println("The input is $it") - } + private fun getRaceTimesPart1(): List = inputData.map { + // Split on spaces, ignore first item, transform tokens to integers + it.split(Regex("\\s")).drop(1).map { it.trim().toLongOrNull() }.filterNotNull() + }.zipWithNext().map { + // Take the zipped lines (since we only have 2) and transform them into races. + it.first.mapIndexed { index, i -> Race(i, it.second[index]) } + }.flatten().also { + println("The input is $it") } override fun partOne() { getRaceTimesPart1().map { // here we transform a race into a list of all the possible combinations that we can have - (1.. Race((it.raceTime - time) * time, it.distance) } - }.parallelStream().map { - // Here we filter out the losing races - it.filter { it.raceTime > it.distance }.count() - }.reduce { acc, i -> acc * i }.get().also { + (1.. (it.raceTime - time) * time > it.distance } + }.reduce { acc, i -> acc * i }.also { println("The number of ways the record was beaten $it.") } } override fun partTwo() { listOf(Race(49787980, 298118510661181)).map { - // here we transform a race into a list of all the possible combinations that we can have - (1.. Race((it.raceTime - time) * time, it.distance) } - }.map { - // Here we filter out the losing races - it.parallelStream().filter { it.raceTime > it.distance }.count() + (1.. (it.raceTime - time) * time > it.distance } }.reduce { acc, i -> acc * i }.also { println("The number of ways the record was beaten $it.") } diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index f9cde91..e6dc394 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -4,7 +4,7 @@ import kotlin.system.measureTimeMillis fun main(args: Array) { val t = WaitForIt() val time = measureTimeMillis { -// t.partOne() + t.partOne() t.partTwo() } println("Took $time ms.")