Skip to content

Commit

Permalink
2024 Day 05 Part1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 5, 2024
1 parent 193cf5d commit b86fd96
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
25 changes: 20 additions & 5 deletions src/main/kotlin/no/rodland/advent_2024/Day05.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,47 @@ import no.rodland.advent.Day
// template generated: 05/12/2024
// Fredrik Rødland 2024

class Day05(val input: List<String>) : Day<Long, Long, Pair<List<Pair<Int, Int>>, List<List<Int>>>> {
class Day05(val input: List<String>) : Day<Int, Long, Pair<List<Pair<Int, Int>>, List<List<Int>>>> {

private val parsed = input.parse()
private val rules = parsed.first
private val pages = parsed.second

override fun partOne(): Long {
return 2
override fun partOne(): Int {
return pages
.filter { list ->
val pairs = list.pairs()
pairs.all { it in rules }
}
.sumOf { it.middle() }
}

override fun partTwo(): Long {
return 2
}

private fun List<Int>.middle(): Int = get(size / 2)

private fun List<Int>.pairs(): List<Pair<Int, Int>> {
return flatMapIndexed { index: Int, i: Int ->
subList(index + 1, size).map { i to it }
}
}


override fun List<String>.parse(): Pair<List<Pair<Int, Int>>, List<List<Int>>> {
val (rulesStr, pagesStr) = this.joinToString("\n").split("\n\n").map { it.split("\n") }

val pages = pagesStr.map { line ->
line.split(",").map { it.toInt() }
}
val rules = rulesStr.map { line ->
val (first, second) = line.split("|").map { it.toInt() }
first to second
line.split("|").map { it.toInt() }.let { it[0] to it[1] }
}
return rules to pages
}

override val day = "05".toInt()
}


6 changes: 4 additions & 2 deletions src/test/kotlin/no/rodland/advent_2024/Day05Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ internal class Day05Test {
private val data05 = "2024/input_05.txt".readFile()
private val test05 = "2024/input_05_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestOne = 143
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultOne = 5762
private val resultTwo = 2L

val test = defaultTestSuiteParseOnInit(
Expand All @@ -29,6 +29,8 @@ internal class Day05Test {
resultTwo,
{ Day05(data05) },
{ Day05(test05) },
numTestPart1 = 10,
numTestPart2 = 10,
)

@Nested
Expand Down

0 comments on commit b86fd96

Please sign in to comment.