🎄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) data class Race(val raceTime: Long, val distance: Long)
class WaitForIt : Puzzle("2023", "6") { class WaitForIt : Puzzle("2023", "6") {
fun getRaceTimesPart1(): List<Race> { private fun getRaceTimesPart1(): List<Race> = inputData.map {
return inputData.map {
// Split on spaces, ignore first item, transform tokens to integers // Split on spaces, ignore first item, transform tokens to integers
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 {
@ -15,27 +14,19 @@ class WaitForIt : Puzzle("2023", "6") {
}.flatten().also { }.flatten().also {
println("The input is $it") println("The input is $it")
} }
}
override fun partOne() { override fun partOne() {
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).count { time -> (it.raceTime - time) * time > it.distance }
}.parallelStream().map { }.reduce { acc, i -> acc * i }.also {
// Here we filter out the losing races
it.filter { it.raceTime > it.distance }.count()
}.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 $it.")
} }
} }
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 (1..<it.raceTime).count { time -> (it.raceTime - time) * time > it.distance }
(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()
}.reduce { acc, i -> acc * i }.also { }.reduce { acc, i -> acc * i }.also {
println("The number of ways the record was beaten $it.") 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>) { fun main(args: Array<String>) {
val t = WaitForIt() val t = WaitForIt()
val time = measureTimeMillis { val time = measureTimeMillis {
// t.partOne() t.partOne()
t.partTwo() t.partTwo()
} }
println("Took $time ms.") println("Took $time ms.")