-
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
0cc663e
commit f87d0d2
Showing
5 changed files
with
90 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
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,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 |
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,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(), | ||
) | ||
|
||
} | ||
|
||
} |
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,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 |