Skip to content

Commit d8d7660

Browse files
committed
solve 16th day 1st task
1 parent d8d038d commit d8d7660

File tree

1 file changed

+49
-0
lines changed
  • year2024/src/main/kotlin/net/olegg/aoc/year2024/day16

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package net.olegg.aoc.year2024.day16
2+
3+
import net.olegg.aoc.someday.SomeDay
4+
import net.olegg.aoc.utils.Directions
5+
import net.olegg.aoc.utils.Directions.Companion.CCW
6+
import net.olegg.aoc.utils.Directions.Companion.CW
7+
import net.olegg.aoc.utils.Vector2D
8+
import net.olegg.aoc.utils.find
9+
import net.olegg.aoc.utils.get
10+
import net.olegg.aoc.year2024.DayOf2024
11+
import java.util.PriorityQueue
12+
13+
/**
14+
* See [Year 2024, Day 16](https://adventofcode.com/2024/day/16)
15+
*/
16+
object Day16 : DayOf2024(16) {
17+
private val empty = setOf('.', 'S', 'E')
18+
override fun first(): Any? {
19+
val start = matrix.find('S')!!
20+
val end = matrix.find('E')!!
21+
22+
val queue = PriorityQueue<Triple<Vector2D, Directions, Int>>(compareBy { it.third })
23+
queue.add(Triple(start, Directions.R, 0))
24+
val seen = mutableSetOf<Pair<Vector2D, Directions>>()
25+
26+
while (queue.isNotEmpty()) {
27+
val point = queue.remove()
28+
val (curr, dir, length) = point
29+
if (curr == end) {
30+
return length
31+
}
32+
val config = curr to dir
33+
if (config in seen) {
34+
continue
35+
}
36+
seen += config
37+
38+
if (matrix[curr + dir.step] in empty) {
39+
queue.add(Triple(curr + dir.step, dir, length + 1))
40+
}
41+
queue.add(Triple(curr, CW[dir]!!, length + 1000))
42+
queue.add(Triple(curr, CCW[dir]!!, length + 1000))
43+
}
44+
45+
return -1
46+
}
47+
}
48+
49+
fun main() = SomeDay.mainify(Day16)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy