Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Day 6 ⭐⭐
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMarquis committed Dec 6, 2023
1 parent 832d254 commit 2a7678e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

| | | | | | | | | | |
|-------------:|:-------------:|--------------:|:---------:|--------------:|:---------:|--------------:|:---------:|--------------:|:---------:|
| **[1][d01]** | **[⭐⭐][s01]** | **[6][d06]** | [☆☆][s06] | **[11][d11]** | [☆☆][s11] | **[16][d16]** | [☆☆][s16] | **[22][d22]** | [☆☆][s22] |
| **[1][d01]** | **[⭐⭐][s01]** | **[6][d06]** | [⭐⭐][s06] | **[11][d11]** | [☆☆][s11] | **[16][d16]** | [☆☆][s16] | **[22][d22]** | [☆☆][s22] |
| **[2][d02]** | **[⭐⭐][s02]** | **[7][d07]** | [☆☆][s07] | **[12][d12]** | [☆☆][s12] | **[17][d17]** | [☆☆][s17] | **[23][d23]** | [☆☆][s23] |
| **[3][d03]** | **[⭐⭐][s03]** | **[8][d08]** | [☆☆][s08] | **[13][d13]** | [☆☆][s13] | **[18][d18]** | [☆☆][s18] | **[24][d24]** | [☆☆][s24] |
| **[4][d04]** | **[⭐⭐][s04]** | **[9][d09]** | [☆☆][s09] | **[14][d14]** | [☆☆][s14] | **[19][d19]** | [☆☆][s19] | **[25][d25]** | [☆☆][s25] |
Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/Day06.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Day06(private val input: List<String>) {

fun part1(): Long = input
.map { """\d+""".toRegex().findAll(it).map(MatchResult::value).map(String::toLong) }
.let { (times, distances) -> (times zip distances) }
.map { (time, distance) -> wins(time, distance) }
.product()

fun part2(): Long = input
.map { it.filter(Char::isDigit).toLong() }
.let { (time, distance) -> wins(time, distance) }

private fun wins(time: Long, distance: Long): Long {
var min = 0
while (min * (time - min) <= distance) min++
var max = time
while (max * (time - max) <= distance) max--
return (max - min + 1)
}

}
5 changes: 5 additions & 0 deletions src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ inline fun <T, R> Sequence<T>.foldIndexedInPlace(
operation: R.(index: Int, T) -> Unit,
): R = foldIndexed(initial) { index: Int, acc: R, t: T -> acc.apply { operation(index, t) } }

@JvmName("intProduct")
fun Sequence<Int>.product(): Long = fold(1L, Long::times)

@JvmName("longProduct")
fun Sequence<Long>.product(): Long = fold(1L, Long::times)
49 changes: 49 additions & 0 deletions src/test/kotlin/Day06Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Resources.readLines
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

@DisplayName("Day 06")
class Day06Test {

private val sampleInput = readLines("Day06-sample.txt")
private val actualInput = readLines("Day06.txt")

@Nested
@DisplayName("Part 1")
inner class Part1 {

@Test
fun `Matches example`() = assertEquals(
expected = 288,
actual = Day06(sampleInput).part1(),
)

@Test
fun `Actual answer`() = assertEquals(
expected = 449820,
actual = Day06(actualInput).part1(),
)

}

@Nested
@DisplayName("Part 2")
inner class Part2 {

@Test
fun `Matches example`() = assertEquals(
expected = 71503,
actual = Day06(sampleInput).part2(),
)

@Test
fun `Actual answer`() = assertEquals(
expected = 42250895,
actual = Day06(actualInput).part2(),
)

}

}
2 changes: 2 additions & 0 deletions src/test/resources/Day06-sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

0 comments on commit 2a7678e

Please sign in to comment.