From 4807b2a0332cf261bdf1f6899662403a39014ba1 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 25 Dec 2024 12:25:32 +0100 Subject: [PATCH] Day 18 part 2 --- 2024/src/main/kotlin/day18.kt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/2024/src/main/kotlin/day18.kt b/2024/src/main/kotlin/day18.kt index 1959f2f..b1ac0f1 100644 --- a/2024/src/main/kotlin/day18.kt +++ b/2024/src/main/kotlin/day18.kt @@ -22,4 +22,28 @@ fun main() { .map { it to 1 } }) println("Part 1: $distance") + + + val input = readInput("day18.txt") + .map { + val (a, b) = it.split(',', limit = 2) + Grid.Coordinate(a.toInt(), b.toInt()) + } + + val mem = CharGrid(size, size, '.') + for (location in input) { + mem[location] = '#' + + val isStopped = dijkstra(start, goal = { it == goal }, neighbors = { pos -> + Direction.entries.asSequence() + .map { pos.step(it) } + .filter { it in mem && mem[it] == '.' } + .map { it to 1 } + }) == null + + if (isStopped) { + println("Part 2: ${location.x},${location.y}") + break + } + } } \ No newline at end of file