Skip to content

Commit

Permalink
Implemented Day 06 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
chebetos committed Dec 9, 2023
1 parent e7305a6 commit 876533e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
32 changes: 25 additions & 7 deletions src/main/kotlin/day06/Day06.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ object Day06 {
}

fun part2(lines: List<String>): Int {
return lines.size
val document = RacesDocument.fromStringWithoutSpaces(lines)
document.println()
return document.calculateMarginOfError()
}
}

Expand All @@ -47,11 +49,11 @@ data class RacesDocument(val races: List<Race>) {
val times = filteredLines[0].removePrefix(TIME_PREFFIX)
.split(' ')
.filter { it.isNotBlank() }
.map { it.toInt() }
.map { it.toLong() }
val distances = filteredLines[1].removePrefix(DISTANCE_PREFFIX)
.split(' ')
.filter { it.isNotBlank() }
.map { it.toInt() }
.map { it.toLong() }

check(times.size == distances.size) { "Invalid input not same amount of time records than distance records: $times vs $distances"}

Expand All @@ -63,12 +65,28 @@ data class RacesDocument(val races: List<Race>) {

return RacesDocument(races)
}

fun fromStringWithoutSpaces(lines: List<String>): RacesDocument {
val filteredLines = lines.filter { it.isNotBlank() }
check(filteredLines.size == 2) { "Invalid input, it should be only 2 lines: $filteredLines"}
check(filteredLines[0].startsWith(TIME_PREFFIX)) { "Invalid input, 1st line doesn't start with 'Time': ${filteredLines[0]}"}
check(filteredLines[1].startsWith(DISTANCE_PREFFIX)) { "Invalid input, 2nd line doesn't start with 'Distance;: ${filteredLines[1]}"}

val time = filteredLines[0].removePrefix(TIME_PREFFIX)
.replace(" ", "")
.toLong()
val distance = filteredLines[1].removePrefix(DISTANCE_PREFFIX)
.replace(" ", "")
.toLong()

return RacesDocument(listOf(Race(time, distance)))
}
}
}

data class Race(val time: Int, val distance: Int) {
fun calculateAchievableDistances(): List<Int> {
val achievableDistances: MutableList<Int> = ArrayList()
data class Race(val time: Long, val distance: Long) {
fun calculateAchievableDistances(): List<Long> {
val achievableDistances: MutableList<Long> = ArrayList()
for (i in 0..time) {
val chargingTime = i
val runningTime = time - i
Expand All @@ -82,7 +100,7 @@ data class Race(val time: Int, val distance: Int) {
fun calculateWaysToWin(): Int {
val achievableDistances = calculateAchievableDistances()
val waysToWin = achievableDistances.indices.filter { achievableDistances[it] > distance }
println("For race: $this,\n\tachievableDistances: $achievableDistances,\n\twaysToWin: ${waysToWin.size} - $waysToWin")
//println("For race: $this,\n\tachievableDistances: $achievableDistances,\n\twaysToWin: ${waysToWin.size} - $waysToWin")
return waysToWin.size
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/day06/Day06Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class Day06Test {

@Test
fun part2() {
assertEquals(2, Day06.part2(testInput))
assertEquals(71503, Day06.part2(testInput))
}
}

0 comments on commit 876533e

Please sign in to comment.