Skip to content

Commit

Permalink
Clean-up day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Dec 5, 2024
1 parent 3a45177 commit 40c2c3d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 46 deletions.
69 changes: 26 additions & 43 deletions 2024/src/main/kotlin/aoc/year2024/Day5.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,41 @@ package aoc.year2024

import aoc.library.Puzzle

object Day5 : Puzzle<Long, Long>(day = 5) {
object Day5 : Puzzle<Int, Int>(day = 5) {

override fun solvePart1(input: String): Long {
// println(input)

val (orderingRulesValue, pagesValue) = input.split("\n\n")

val orderingRules = orderingRulesValue.lines().map {
val (l, r) = it.split("|")
l.toLong() to r.toLong()
}

val pages = pagesValue.lines()
.map {
it.split(",").map { it.toLong() }
}

val comparator = Comparator<Long> { o1, o2 ->
val rule = orderingRules.first { (it.first == o1 && it.second == o2) || (it.first == o2 && it.second == o1) }
if (o1 == rule.first) -1 else 1
}
override fun solvePart1(input: String): Int {
val (pages, comparator) = parse(input)
return pages
.filter { it.sortedWith(comparator) == it }
.sumOf { it[it.count() / 2] }
}

override fun solvePart2(input: String): Int {
val (pages, comparator) = parse(input)
return pages
.filter {
it.sortedWith(comparator) == it
}
.filter { it.sortedWith(comparator) != it }
.map { it.sortedWith(comparator) }
.sumOf { it[it.count() / 2] }
}

override fun solvePart2(input: String): Long {
private fun parse(input: String): Pair<List<List<Int>>, Comparator<Int>> {
val (orderingRulesValue, pagesValue) = input.split("\n\n")
return getPages(pagesValue) to comparator(orderingRulesValue)
}

val orderingRules = orderingRulesValue.lines().map {
val (l, r) = it.split("|")
l.toLong() to r.toLong()
}

val pages = pagesValue.lines()
.map {
it.split(",").map { it.toLong() }
}
private fun getPages(pagesValue: String) = pagesValue.lines().map { page ->
page.split(",").map(String::toInt)
}

val comparator = Comparator<Long> { o1, o2 ->
val rule = orderingRules.first { (it.first == o1 && it.second == o2) || (it.first == o2 && it.second == o1) }
if (o1 == rule.first) -1 else 1
private fun comparator(orderingRulesValue: String): Comparator<Int> {
val orderingRules = mutableMapOf<Pair<Int, Int>, Int>()
orderingRulesValue.lines().forEach {
val (l, r) = it.split("|").map(String::toInt)
orderingRules[l to r] = -1
orderingRules[r to l] = 1
}
return Comparator { o1, o2 ->
orderingRules.getValue(o1 to o2)
}

return pages
.filter {
it.sortedWith(comparator) != it
}
.map { it.sortedWith(comparator) }
.sumOf { it[it.count() / 2] }
}
}
6 changes: 3 additions & 3 deletions 2024/src/test/kotlin/aoc/year2024/Day5Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package aoc.year2024

import aoc.library.solvePart1
import aoc.library.solvePart2
import io.kotest.matchers.longs.shouldBeExactly
import io.kotest.matchers.ints.shouldBeExactly
import org.junit.jupiter.api.Test

class Day5Test {
Expand Down Expand Up @@ -44,7 +44,7 @@ class Day5Test {

@Test
fun part1() {
Day5.solvePart1() shouldBeExactly 42
Day5.solvePart1() shouldBeExactly 5747
}

@Test
Expand Down Expand Up @@ -84,6 +84,6 @@ class Day5Test {

@Test
fun part2() {
Day5.solvePart2() shouldBeExactly 42
Day5.solvePart2() shouldBeExactly 5502
}
}

0 comments on commit 40c2c3d

Please sign in to comment.