day05
This commit is contained in:
65
app/src/main/kotlin/day05/Day05.kt
Normal file
65
app/src/main/kotlin/day05/Day05.kt
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package day05
|
||||||
|
|
||||||
|
import kotlin.io.println
|
||||||
|
import kotlin.collections.listOf
|
||||||
|
|
||||||
|
class Day05 {
|
||||||
|
fun parse(input: String): Pair<HashMap<Int, List<Int>>, List<List<Int>>> {
|
||||||
|
val (ruleStr, updateStr) = input.split("\n\n").map { it.lines() }
|
||||||
|
var rules = HashMap<Int, List<Int>>()
|
||||||
|
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<Int, List<Int>>, update: List<Int>): Boolean {
|
||||||
|
for (i in 0..<update.count()) {
|
||||||
|
if (rules[update[i]]?.map { update.subList(0, i).contains(it) }?.contains(true) ?: false) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fixOrder(rules: HashMap<Int, List<Int>>, update: List<Int>): List<Int> {
|
||||||
|
var modUpdate = update.toMutableList()
|
||||||
|
for (i in 0..<update.count()) {
|
||||||
|
val curPage = modUpdate[i]
|
||||||
|
val curRule = rules[curPage]
|
||||||
|
if (curRule == null) { continue }
|
||||||
|
for (pre in 0..<i) {
|
||||||
|
if (curRule.contains(modUpdate[pre])) {
|
||||||
|
modUpdate[i] = modUpdate[pre]
|
||||||
|
modUpdate[pre] = curPage
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// hate this but im lazy
|
||||||
|
if (!checkOrder(rules, modUpdate)) {
|
||||||
|
modUpdate = fixOrder(rules, modUpdate).toMutableList()
|
||||||
|
}
|
||||||
|
return modUpdate
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part2(input: String): Int {
|
||||||
|
val (rules, updates) = parse(input)
|
||||||
|
return updates.filter {update -> !checkOrder(rules, update) }
|
||||||
|
.map { update -> fixOrder(rules, update) }
|
||||||
|
.map { update -> update[update.count() / 2] }
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
41
app/src/test/kotlin/day05/Day05Test.kt
Normal file
41
app/src/test/kotlin/day05/Day05Test.kt
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user