From 4d189a1a28e789f2b47a540a76a7dee12e75e39e Mon Sep 17 00:00:00 2001 From: Denis-Cosmin NUTIU Date: Wed, 6 Dec 2023 12:29:27 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=84Advent=20of=20Code=20-=20Day=206=20?= =?UTF-8?q?=E2=9C=A8part=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/AdventOfCode2023/day6/WaitForIt.kt | 41 +++++++++++++++++++ src/main/kotlin/Main.kt | 8 ++-- src/main/resources/aoc2023/input_day6.txt | 2 + 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/AdventOfCode2023/day6/WaitForIt.kt create mode 100644 src/main/resources/aoc2023/input_day6.txt diff --git a/src/main/kotlin/AdventOfCode2023/day6/WaitForIt.kt b/src/main/kotlin/AdventOfCode2023/day6/WaitForIt.kt new file mode 100644 index 0000000..25e09c4 --- /dev/null +++ b/src/main/kotlin/AdventOfCode2023/day6/WaitForIt.kt @@ -0,0 +1,41 @@ +package AdventOfCode2023.day6 + +import AdventOfCode.Puzzle +import java.util.stream.Collectors + +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") } + } + + override fun partOne() { + val raceTimes = 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() + + println("The number of ways the record was beaten $raceTimes.") + } + + 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) }.collect( + Collectors.toList()) + }.map { + // Here we filter out the losing races + it.parallelStream().filter { it.raceTime > it.distance }.count() + }.reduce { acc, i -> acc * i }.also { println("The number of ways the record was beaten $it.") } + } +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 63dc290..f9cde91 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,11 +1,11 @@ -import AdventOfCode2023.day4.Scratchcards +import AdventOfCode2023.day6.WaitForIt import kotlin.system.measureTimeMillis fun main(args: Array) { - val t = Scratchcards() - // t.partOne() + val t = WaitForIt() val time = measureTimeMillis { - t.partTwo() +// t.partOne() + t.partTwo() } println("Took $time ms.") } \ No newline at end of file diff --git a/src/main/resources/aoc2023/input_day6.txt b/src/main/resources/aoc2023/input_day6.txt new file mode 100644 index 0000000..da82459 --- /dev/null +++ b/src/main/resources/aoc2023/input_day6.txt @@ -0,0 +1,2 @@ +Time: 49787980 +Distance: 298118510661181 \ No newline at end of file