File tree Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ package y24
2+
3+ import common.puzzle.solvePuzzle
4+ import common.puzzle.Input
5+ import common.puzzle.Puzzle
6+ import common.datastructures.*
7+ import common.ext.*
8+ import common.util.*
9+ import java.util.*
10+ import kotlin.math.*
11+ import kotlin.system.exitProcess
12+
13+
14+ fun main () = solvePuzzle(year = 2024 , day = 10 ) { Day10 (it) }
15+
16+ class Day10 (val input : Input ) : Puzzle {
17+
18+ private fun trails (grid : Grid <Int >, p : Cell <Int >): List <Point > {
19+ if (p.value == 9 ) {
20+ return listOf (p.toPoint())
21+ }
22+
23+ return grid.neighbors(p)
24+ .filter { it.value == p.value + 1 }
25+ .flatMap { trails(grid, it) }
26+ }
27+
28+ private val grid = Grid (input.lines.size, input.lines[0 ].length) { row, col -> input.lines[row][col].digitToInt() }
29+ private val trailHeads = grid.cells().filter { it.value == 0 }
30+
31+ override fun solveLevel1 (): Any {
32+ return trailHeads.sumOf { trails(grid, it).distinct().size }
33+ }
34+
35+ override fun solveLevel2 (): Any {
36+ return trailHeads.sumOf { trails(grid, it).size }
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package y24
2+
3+ import common.puzzle.Input
4+ import org.junit.jupiter.api.Assertions.assertEquals
5+ import org.junit.jupiter.api.Test
6+
7+ internal class Day10Test {
8+ private val sample = Input ("""
9+ 89010123
10+ 78121874
11+ 87430965
12+ 96549874
13+ 45678903
14+ 32019012
15+ 01329801
16+ 10456732
17+ """ .trimIndent())
18+
19+ private val day = Day10 (sample)
20+
21+ @Test
22+ fun solveLevel1 () {
23+ assertEquals(36 , day.solveLevel1())
24+ }
25+
26+ @Test
27+ fun solveLevel2 () {
28+ assertEquals(81 , day.solveLevel2())
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments