Skip to content

Commit

Permalink
Day 7 ⭐⭐
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMarquis committed Dec 7, 2024
1 parent 0cc663e commit f87d0d2
Show file tree
Hide file tree
Showing 5 changed files with 90 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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repositories {
}

dependencies {
implementation("com.github.shiguruikai:combinatoricskt:1.6.0")
testImplementation(kotlin("test"))
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/kotlin/Day07.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import com.github.shiguruikai.combinatoricskt.permutationsWithRepetition

class Day07(input: List<String>) {

private val equations: List<Equation> = input
.map { it.split(": ") }
.map { (a, b) -> a.toLong() to b.split(" ").map(String::toLong) }

private val plus: Operation = Long::plus
private val times: Operation = Long::times
private val concat: Operation = { a, b -> (a.toString() + b.toString()).toLong() }

fun part1() = solve(setOf(plus, times))
fun part2() = solve(setOf(plus, times, concat))

private fun solve(operations: Set<Operation>) = equations
.filter { it(operations) }
.sumOf { it.value }

private operator fun Equation.invoke(operations: Set<Operation>): Boolean = operations
.permutationsWithRepetition(numbers.size - 1)
.any { value == numbers.reduceIndexed { index, acc, i -> it[index - 1](acc, i) } }

}

typealias Equation = Pair<Long, List<Long>>
typealias Operation = (Long, Long) -> Long

val Equation.value get() = first
val Equation.numbers get() = second
49 changes: 49 additions & 0 deletions src/test/kotlin/Day07Test.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 07")
class Day07Test {

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

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

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

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

}

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

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

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

}

}
9 changes: 9 additions & 0 deletions src/test/resources/Day07-sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20

0 comments on commit f87d0d2

Please sign in to comment.