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

Commit

Permalink
Day 2 ⭐⭐
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMarquis committed Dec 2, 2023
1 parent 2c4bb3b commit 3e3ffd1
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
| | | | | | | | | | |
|-------------:|:-------------:|--------------:|:---------:|--------------:|:---------:|--------------:|:---------:|--------------:|:---------:|
| **[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] |
| **[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] |
| **[5][d05]** | **[☆☆][s05]** | **[10][d10]** | [☆☆][s10] | **[15][d15]** | [☆☆][s15] | **[20][d20]** | [☆☆][s20] | | |
Expand Down
32 changes: 32 additions & 0 deletions src/main/kotlin/Day02.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Day02.Color.blue
import Day02.Color.green
import Day02.Color.red

class Day02(private val input: List<String>) {

@Suppress("EnumEntryName")
enum class Color { red, green, blue }

private val regex = """(\d+) (${Color.entries.joinToString("|")})""".toRegex()

fun part1(
target: Map<Color, Int> = mapOf(red to 12, green to 13, blue to 14),
): Int = input.withIndex().filter { (_, line) ->
line.split(";")
.map { it.cubes() }
.all { it.validate(target) }
}.sumOf { it.index + 1 }

fun part2() = input.withIndex().sumOf { (_, line) ->
val cubes = line.split(";").map { it.cubes() }
fun maxOf(color: Color) = cubes.maxOf { it.getValue(color) }
maxOf(red) * maxOf(green) * maxOf(blue)
}

private fun String.cubes(): Map<Color, Int> = regex.findAll(this)
.map { it.destructured.let { (amount, color) -> Color.valueOf(color) to amount.toInt() } }
.foldInPlace(mutableMapOf<Color, Int>().withDefault { 0 }) { (color, amount) -> merge(color, amount, Int::plus) }

private fun Map<Color, Int>.validate(target: Map<Color, Int>) = target.all { (key, value) -> getValue(key) <= value }

}
5 changes: 5 additions & 0 deletions src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
inline fun <T, R> Sequence<T>.foldInPlace(
initial: R,
operation: R.(T) -> Unit,
): R = fold(initial) { acc: R, t: T -> acc.apply { operation(t) } }

54 changes: 54 additions & 0 deletions src/test/kotlin/Day02Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Day02.Color.blue
import Day02.Color.green
import Day02.Color.red
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 02")
class Day02Test {

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

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

@Test
fun `Matches example`() {
assertEquals(
expected = 8,
actual = Day02(sampleInput).part1(),
)
}

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

}

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

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

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

}

}
5 changes: 5 additions & 0 deletions src/test/resources/Day02-sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

0 comments on commit 3e3ffd1

Please sign in to comment.