From 2f399ce7909b442cb0faa3bdf4a815d8213daf90 Mon Sep 17 00:00:00 2001 From: Andrew Glaze Date: Sat, 7 Dec 2024 14:06:08 -0500 Subject: [PATCH] day05 --- app/src/main/kotlin/day05/Day05.kt | 65 ++++++++++++++++++++++++++ app/src/test/kotlin/day05/Day05Test.kt | 41 ++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 app/src/main/kotlin/day05/Day05.kt create mode 100644 app/src/test/kotlin/day05/Day05Test.kt diff --git a/app/src/main/kotlin/day05/Day05.kt b/app/src/main/kotlin/day05/Day05.kt new file mode 100644 index 0000000..b6eec22 --- /dev/null +++ b/app/src/main/kotlin/day05/Day05.kt @@ -0,0 +1,65 @@ +package day05 + +import kotlin.io.println +import kotlin.collections.listOf + +class Day05 { + fun parse(input: String): Pair>, List>> { + val (ruleStr, updateStr) = input.split("\n\n").map { it.lines() } + var rules = HashMap>() + ruleStr.map { + val split = it.split("|").map { it.toInt() } + val cur = rules[split[0]] ?: listOf() + rules.set(split[0], cur + split[1]) + } + + val updates = updateStr.map { it.split(",").map { it.toInt() } } + return Pair(rules, updates) + } + + fun part1(input: String): Int { + val (rules, updates) = parse(input) + return updates.filter {update -> checkOrder(rules, update) } + .map {update -> update[update.count() / 2] } + .sum() + } + + fun checkOrder(rules: HashMap>, update: List): Boolean { + for (i in 0..>, update: List): List { + var modUpdate = update.toMutableList() + for (i in 0.. !checkOrder(rules, update) } + .map { update -> fixOrder(rules, update) } + .map { update -> update[update.count() / 2] } + .sum() + } +} + diff --git a/app/src/test/kotlin/day05/Day05Test.kt b/app/src/test/kotlin/day05/Day05Test.kt new file mode 100644 index 0000000..7fe7172 --- /dev/null +++ b/app/src/test/kotlin/day05/Day05Test.kt @@ -0,0 +1,41 @@ +package day05 + +import kotlin.test.Test +import util.InputDownloader + +class Day05Test { + val dayNum = 5 + val day = Day05() + + val input = InputDownloader().getInput(dayNum) + val example = InputDownloader().getExample(dayNum) + + @Test fun part1Example() { + part1(example, 143) + } + + @Test fun part1Solution() { + part1(input, 6041) + } + + @Test fun part2Example() { + part2(example, 123) + } + + @Test fun part2Solution() { + part2(input, 4884) + } + + + fun part1(input: String, expected: Int) { + val output = day.part1(input) + println("output: $output") + assert(output == expected) + } + + fun part2(input: String, expected: Int) { + val output = day.part2(input) + println("output: $output") + assert(output == expected) + } +}