Skip to content

Commit

Permalink
2024 - Day 10 - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 10, 2024
1 parent 93c7ea9 commit 58b69bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
43 changes: 35 additions & 8 deletions src/main/kotlin/no/rodland/advent_2024/Day10.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
package no.rodland.advent_2024

import no.rodland.advent.Day
import no.rodland.advent.Pos

// template generated: 10/12/2024
// Fredrik Rødland 2024

class Day10(val input: List<String>) : Day<Long, Long, List<String>> {
class Day10(val input: List<String>) : Day<Int, Long, Pair<Pair<Set<Pos>, Set<Pos>>, Array<CharArray>>> {

private val parsed = input.parse()
private val heads = parsed.first.first
private val tails = parsed.first.second
private val grid = parsed.second

override fun partOne(): Long {
return 2
override fun partOne(): Int {
return heads.sumOf { it.score(mutableSetOf()) }
}

override fun partTwo(): Long {
return 2
}

override fun List<String>.parse(): List<String> {
return map { line ->
line
}
private fun Pos.score(visited: MutableSet<Pos>): Int {
visited.add(this)
val c = grid[this].digitToInt()
return if (c == 9) return 1
else neighbourCellsUDLR()
.filterNot { it in visited }
.filter { it in grid }
.filter { grid[it].digitToInt() == c + 1 }
.sumOf { it.score(visited) }
}

operator fun Grid.contains(pos: Pos): Boolean = pos.x >= 0 && pos.x < this[0].size && pos.y >= 0 && pos.y < this.size

operator fun Grid.get(pos: Pos): Char = this[pos.y][pos.x]

override fun List<String>.parse(): Pair<Pair<Set<Pos>, Set<Pos>>, Array<CharArray>> {
val heads = mutableSetOf<Pos>()
val tails = mutableSetOf<Pos>()
val grid = indices.map { y ->
indices.map { x ->
val c = this[y][x]
if (c == '0') heads.add(Pos(x, y))
if (c == '9') tails.add(Pos(x, y))
c
}.toCharArray()
}.toTypedArray()
return (heads to tails) to grid
}

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

5 changes: 3 additions & 2 deletions src/test/kotlin/no/rodland/advent_2024/Day10Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ internal class Day10Test {
private val data10 = "2024/input_10.txt".readFile()
private val test10 = "2024/input_10_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestOne = 36
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultOne = 514
private val resultTwo = 2L

val test = defaultTestSuiteParseOnInit(
Expand All @@ -29,6 +29,7 @@ internal class Day10Test {
resultTwo,
{ Day10(data10) },
{ Day10(test10) },
numTestPart1 = 200
)

@Nested
Expand Down

0 comments on commit 58b69bb

Please sign in to comment.