Skip to content

Commit d1be6be

Browse files
committed
Optimize 2023 Day 21 Part 1 solution
1 parent 9acbd11 commit d1be6be

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

adventofcode2023/Day21.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@ object Day21 {
4141

4242
private fun stepPossibilities(numberOfSteps: Int): Int {
4343
val steps = LinkedList(listOf(startingStep()))
44+
val possible = mutableListOf<Vector2>()
4445

4546
while (steps.isNotEmpty()) {
4647
val current = steps.removeFirst()
4748

48-
if (current.stepCount == numberOfSteps) return steps.size + 1
49-
else steps.addAll(neighbors(current).filterNot { steps.contains(it) })
49+
if (current.stepCount > numberOfSteps) break
50+
if ((current.stepCount + numberOfSteps) % 2 == 0) possible.add(current.position)
51+
52+
steps.addAll(neighbors(current).filterNot { steps.contains(it) || possible.contains(it.position) })
5053
}
5154

52-
throw Exception("Couldn't find the number of step possibilities for $numberOfSteps steps")
55+
return possible.size
5356
}
5457

5558
fun part1() = println(stepPossibilities(64))

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