From c8a967dcaaf937fff1a2e064d2b221c1acb708d0 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 6 Feb 2024 11:10:29 +0500 Subject: [PATCH] aoc-23: solve day 6 --- .../src/main/kotlin/Solution06.kt | 66 +++++++++++++++++++ .../src/main/resources/day-06/input.txt | 2 + .../src/main/resources/day-06/test.txt | 2 + .../src/test/kotlin/Solution06Test.kt | 33 ++++++++++ 4 files changed, 103 insertions(+) create mode 100644 advent-of-code-2023/src/main/kotlin/Solution06.kt create mode 100644 advent-of-code-2023/src/main/resources/day-06/input.txt create mode 100644 advent-of-code-2023/src/main/resources/day-06/test.txt create mode 100644 advent-of-code-2023/src/test/kotlin/Solution06Test.kt diff --git a/advent-of-code-2023/src/main/kotlin/Solution06.kt b/advent-of-code-2023/src/main/kotlin/Solution06.kt new file mode 100644 index 0000000..322d507 --- /dev/null +++ b/advent-of-code-2023/src/main/kotlin/Solution06.kt @@ -0,0 +1,66 @@ +import kotlin.math.ceil +import kotlin.math.sqrt + +class Solution06(lines: List) { + private val races = mutableListOf() + + init { + val times = lines[0] + .replace("Time:", "") + .split(" ") + .filter{it.isNotEmpty()} + val records = lines[1] + .replace("Distance:", "") + .split(" ") + .filter{it.isNotEmpty()} + + for (i in times.indices) { + races.add( + Race(records[i].toLong(), times[i].toLong()) + ) + } + } + + fun getMultipliedNumbersOfWays(): Int { + var res = 1 + + println("races: $races") + + for (race in races) { + var x1 = (race.duration - sqrt(race.duration * race.duration - 4.0 * race.distance)) / 2.0 + val x2 = (race.duration + sqrt(race.duration * race.duration - 4.0 * race.distance)) / 2.0 + + if (ceil(x1) - x1 < 1e-4) { + x1 += 0.5 + } + + println("x1 = $x1, x2 = $x2") + + res *= (ceil(x2) - ceil(x1)).toInt() + } + + return res + } + + fun getNumberOfWaysForOneRace(): Int { + val race = Race( + races.map{it.distance.toString()}.reduce{acc, s -> acc + s}.toLong(), + races.map{it.duration.toString()}.reduce{acc, s -> acc + s}.toLong(), + ) + + println("distance: ${race.distance}, duration: ${race.duration}") + + var x1 = (race.duration - sqrt(race.duration * race.duration - 4.0 * race.distance)) / 2.0 + val x2 = (race.duration + sqrt(race.duration * race.duration - 4.0 * race.distance)) / 2.0 + + if (ceil(x1) - x1 < 1e-4) { + x1 += 0.5 + } + + println("x1 = $x1, x2 = $x2") + + return (ceil(x2) - ceil(x1)).toInt() + } +} + +data class Race(val distance: Long, val duration: Long) diff --git a/advent-of-code-2023/src/main/resources/day-06/input.txt b/advent-of-code-2023/src/main/resources/day-06/input.txt new file mode 100644 index 0000000..2a292a0 --- /dev/null +++ b/advent-of-code-2023/src/main/resources/day-06/input.txt @@ -0,0 +1,2 @@ +Time: 47 84 74 67 +Distance: 207 1394 1209 1014 diff --git a/advent-of-code-2023/src/main/resources/day-06/test.txt b/advent-of-code-2023/src/main/resources/day-06/test.txt new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/advent-of-code-2023/src/main/resources/day-06/test.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 diff --git a/advent-of-code-2023/src/test/kotlin/Solution06Test.kt b/advent-of-code-2023/src/test/kotlin/Solution06Test.kt new file mode 100644 index 0000000..d0fd0be --- /dev/null +++ b/advent-of-code-2023/src/test/kotlin/Solution06Test.kt @@ -0,0 +1,33 @@ +import kotlin.test.Test +import kotlin.test.assertEquals + +class Solution06Test{ + + @Test + fun testGetMultipliedNumbersOfWays() { + val text = ResourceReader().readFile("day-06/test.txt") + val res = Solution06(text).getMultipliedNumbersOfWays() + assertEquals(288, res) + } + + @Test + fun solvePart1() { + val text = ResourceReader().readFile("day-06/input.txt") + val res = Solution06(text).getMultipliedNumbersOfWays() + println(res) + } + + @Test + fun testGetNumberOfWaysForOneRace() { + val text = ResourceReader().readFile("day-06/test.txt") + val res = Solution06(text).getNumberOfWaysForOneRace() + assertEquals(71503, res) + } + + @Test + fun solvePart2() { + val text = ResourceReader().readFile("day-06/input.txt") + val res = Solution06(text).getNumberOfWaysForOneRace() + println(res) + } +}