-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters