🎄Advent of Code - Refactor Day 6 with JetBrains AI

This commit is contained in:
Denis-Cosmin Nutiu 2023-12-06 23:57:33 +02:00
parent cf840f82aa
commit b3a8f7d331
2 changed files with 12 additions and 21 deletions

View file

@ -5,8 +5,7 @@ import AdventOfCode.Puzzle
data class Race(val raceTime: Long, val distance: Long)
class WaitForIt : Puzzle("2023", "6") {
fun getRaceTimesPart1(): List<Race> {
return inputData.map {
private fun getRaceTimesPart1(): List<Race> = 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 {
@ -15,27 +14,19 @@ class WaitForIt : Puzzle("2023", "6") {
}.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..<it.raceTime).map { time -> 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).count { time -> (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..<it.raceTime).map { time -> 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).count { time -> (it.raceTime - time) * time > it.distance }
}.reduce { acc, i -> acc * i }.also {
println("The number of ways the record was beaten $it.")
}

View file

@ -4,7 +4,7 @@ import kotlin.system.measureTimeMillis
fun main(args: Array<String>) {
val t = WaitForIt()
val time = measureTimeMillis {
// t.partOne()
t.partOne()
t.partTwo()
}
println("Took $time ms.")