Skip to content

Commit d026f62

Browse files
committed
Merge branch 'feature/day20' into develop
2 parents d900302 + a328c49 commit d026f62

File tree

1 file changed

+64
-0
lines changed
  • year2024/src/main/kotlin/net/olegg/aoc/year2024/day20

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package net.olegg.aoc.year2024.day20
2+
3+
import net.olegg.aoc.someday.SomeDay
4+
import net.olegg.aoc.utils.Directions.Companion.NEXT_4
5+
import net.olegg.aoc.utils.Vector2D
6+
import net.olegg.aoc.utils.find
7+
import net.olegg.aoc.utils.get
8+
import net.olegg.aoc.utils.pairs
9+
import net.olegg.aoc.year2024.DayOf2024
10+
11+
/**
12+
* See [Year 2024, Day 20](https://adventofcode.com/2024/day/20)
13+
*/
14+
object Day20 : DayOf2024(20) {
15+
override fun first(): Any? {
16+
return solve(2)
17+
}
18+
19+
override fun second(): Any? {
20+
return solve(20)
21+
}
22+
23+
private fun solve(maxCheat: Int, minSave: Int = 100): Int {
24+
val start = matrix.find('S')!!
25+
val end = matrix.find('E')!!
26+
27+
val queue = ArrayDeque(listOf(start to 0))
28+
val seen = mutableMapOf<Vector2D, Int>()
29+
30+
while (queue.isNotEmpty()) {
31+
val (curr, step) = queue.removeFirst()
32+
33+
if (curr in seen) {
34+
continue
35+
}
36+
seen[curr] = step
37+
if (curr == end) {
38+
break
39+
}
40+
41+
queue += NEXT_4.map { curr + it.step }
42+
.filter { it == end || matrix[it] == '.'}
43+
.filter { it !in seen }
44+
.map { it to step + 1 }
45+
}
46+
47+
val path = generateSequence(end to seen[end]!!) { (curr, step) ->
48+
NEXT_4.map { curr + it.step }
49+
.firstOrNull { seen[it] == step - 1 }
50+
?.let { it to step - 1 }
51+
}.toList().reversed()
52+
53+
return path.pairs().count { (first, second) ->
54+
val (firstPos, firstStep) = first
55+
val (secondPos, secondStep) = second
56+
57+
val delta = (secondPos - firstPos).manhattan()
58+
59+
delta <= maxCheat && secondStep - firstStep - delta >= minSave
60+
}
61+
}
62+
}
63+
64+
fun main() = SomeDay.mainify(Day20)

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