From fb7549498eef76f920dc248b3fb70dc21f0dfbbd Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 11 Dec 2024 20:45:49 +0100 Subject: [PATCH] Day 10 part 2 --- 2024/src/main/kotlin/day10.kt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/2024/src/main/kotlin/day10.kt b/2024/src/main/kotlin/day10.kt index 5b0ee5a..1bfb32e 100644 --- a/2024/src/main/kotlin/day10.kt +++ b/2024/src/main/kotlin/day10.kt @@ -7,6 +7,13 @@ fun main() { } println("Part 1: $part1") + + val part2 = map.findCoordinatesOf(0) + .fold(0) { acc, start -> + acc + map.pathRating(start) + } + + println("Part 2: $part2") } fun Grid.findCoordinatesOf(value: T) = sequence { @@ -32,4 +39,19 @@ fun Grid.pathsToTop(coordinate: Grid.Coordinate): Set { .fold(emptySet()) { acc, next -> acc + pathsToTop(next) } +} + +fun Grid.pathRating(coordinate: Grid.Coordinate): Int { + val height = get(coordinate) + + if (height == 9) { + return 1 + } + + return Direction.entries + .map { coordinate.step(it) } + .filter { it in this && this[it] == height + 1 } + .fold(0) { acc, next -> + acc + pathRating(next) + } } \ No newline at end of file