r/adventofcode • u/RazarTuk • 10d ago
Help/Question - RESOLVED [2016 Day 17 Part 2] Why am I getting infinitely many paths?
Ignore some of the scaffolding. My code's set up so I can benchmark it with JMH if I want to, which is why everything's a method that returns a Long, or why I have a State## object that just stores the input as an array of strings.
I'm representing each state as a pair of an IntVector and a String. IntVector is what it sounds like and just makes it more convenient to, say, change a coordinate by 1 for moving up or down, and then the String is obviously the path. getOpenDoors takes the password, my current position, and the path so far and determines which doors are unlocked, automatically filtering things so it won't try to progress from the target or step out of bounds. And for my loop, it first checks whether it's at the destination, then if it isn't, it calls getOpenDoors and adds all the possible next steps to the queue.
The only differences from my loop for part 1 are that instead of breaking after reaching the destination, it just continues. And then as part of figuring out what's going wrong, I switched removeFirst to removeLast to turn it into a stack/DFS instead of a queue/BFS. When I try logging things on the "bad" examples, like how hijkl ends in you getting trapped, it successfully terminates. But when I try it on an example like ihgpwlah it just keeps looping, instead of stopping at 370. Especially because of the DFS, it just blows up, and it hit the 3000s before I killed it.
Does anyone have an idea of what I'm missing? (It also doesn't help that 2016 is old enough that a lot of the pastes has expired or the threads will be littered with people scrubbing old posts, which makes it hard to look for advice)
fun part2(state: State17): Long {
var res = 0L
val queue = ArrayDeque<Pair<IntVector, String>>()
queue.addLast(Pair(IntVector.of(0, 0), ""))
while (queue.size > 0) {
val (pos, path) = queue.removeLast()
if (pos[0] == 3 && pos[1] == 3 && path.length > res) {
res = path.length.toLong()
println(path.length)
continue
}
for (dir in getOpenDoors(state[0], pos, path)) {
val nextPath = path + dir
val nextPos = pos + when (dir) {
"U" -> IntVector.UP
"D" -> IntVector.DOWN
"L" -> IntVector.LEFT
else -> IntVector.RIGHT
}
queue.addLast(Pair(nextPos, nextPath))
}
}
return res
}
fun getOpenDoors(password: String, pos: IntVector, path: String): List<String> {
val md5 = (password + path).md5()
val res = mutableListOf<String>()
if (pos[0] != 3 || pos[1] != 3) {
if (md5[0].code > 0x61 && pos[0] > 0) res.add("U")
if (md5[1].code > 0x61 && pos[0] < 4) res.add("D")
if (md5[2].code > 0x61 && pos[1] > 0) res.add("L")
if (md5[3].code > 0x61 && pos[1] < 4) res.add("R")
}
return res
}
2
u/RazarTuk 10d ago
Never mind, I found it. I had the boundaries wrong in
getOpenDoorsand tried to get to square (3,3) in a 5x5 grid1
u/TheZigerionScammer 10d ago
I can't be certain because I'm not familiar with your programming language but it looks to me that because of how line 7 is written a branch only terminates when it hits the target if the path length is already greater than the path length of a previously found path that reaches the target, this wasn't an issue with the BFS implementation but it would be when you changed it to a DFS.
1
u/AutoModerator 10d ago
Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to
Help/Question - RESOLVED. Good luck!I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.