This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c4bb3b
commit 3e3ffd1
Showing
5 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |