diff --git a/app/src/main/kotlin/day07/Day07.kt b/app/src/main/kotlin/day07/Day07.kt new file mode 100644 index 0000000..c50803a --- /dev/null +++ b/app/src/main/kotlin/day07/Day07.kt @@ -0,0 +1,63 @@ +package day07 + +import kotlin.io.println +import kotlin.collections.listOf +import kotlin.coroutines.* + +class Day07 { + fun parse(input: String): List>> { + return input.lines().map { line -> + val split = line.split(":") + Pair(split[0].toLong(), split[1].trim().split(" ").map { it.toLong() }) + } + } + + fun part1(input: String): Long { + return parse(input) + .filter { tryOperations(it, listOf("+", "*")) } + .map { it.first } + .sum() + } + + fun tryOperations(equation: Pair>, operations: List): Boolean { + var operPermutations: List> = listOf(listOf()) + for (i in 0.. total += equation.second[i] + "*" -> total *= equation.second[i] + "||" -> total = "${total}${equation.second[i]}".toLong() + } + } + if (total == equation.first) { + return true + } + } + return false + } + + fun getCombos(combos: List>, items: List): List> { + var newCombos: MutableList> = mutableListOf() + for (combo in combos) { + for (item in items) { + newCombos.add(combo + item) + } + } + return newCombos + } + + fun part2(input: String): Long { + return parse(input) + .filter { + tryOperations(it, listOf("+", "*", "||")) + } + .map { it.first } + .sum() + } +} + + diff --git a/app/src/test/kotlin/day07/Day07Test.kt b/app/src/test/kotlin/day07/Day07Test.kt new file mode 100644 index 0000000..7869460 --- /dev/null +++ b/app/src/test/kotlin/day07/Day07Test.kt @@ -0,0 +1,41 @@ +package day07 + +import kotlin.test.Test +import util.InputDownloader + +class Day07Test { + val dayNum = 7 + val day = Day07() + + val input = InputDownloader().getInput(dayNum) + val example = InputDownloader().getExample(dayNum) + + @Test fun part1Example() { + part1(example, 3749) + } + + @Test fun part1Solution() { + part1(input, 1289579105366) + } + + @Test fun part2Example() { + part2(example, 11387) + } + + @Test fun part2Solution() { + part2(input, 92148721834692) + } + + fun part1(input: String, expected: Long) { + val output = day.part1(input) + println("output: $output") + assert(output == expected) + } + + fun part2(input: String, expected: Long) { + val output = day.part2(input) + println("output: $output") + assert(output == expected) + } +} +