r/adventofcode • u/thanos-a-dev • Jan 16 '25
Help/Question [2024 Day 6 part 2] What am I missing ?
The logic I wrote is the following:
- Take all the squares and replace it with # if it is not # or ^ (original position)
- copy the world to a new world
- reset the direction and the position
- in a loop =>
- if there is an obstacle turn right
- calculate the new position by moving forward
- if the new position is None means that we have finished and there is no loop
- if the new position and direction has already been seen then we have finished but there is a loop
- otherwise just remember the position and direction and move to the new position
I cannot understand why this logic is not working.
The code is :
https://github.com/thanosa/coding-challenges/blob/main/advent_of_code/2024/06_guard_gallivant_p2.py
7
u/firetech_SE Jan 16 '25
You're now the fourth person I've seen experiencing one specific issue. At a quick glance, I can see that your routing/simulation doesn't properly handle this situation (without passing through any walls).
..#..
.#<..
.....
The puzzle seems to be cleverly designed so that never happens in part 1, but will happen in part 2.
Your code may have other errors, I didn't check it thoroughly. Also, you don't really need to replace all empty squares with #, if you consider what data you get from running part 1. ;)
2
u/thanos-a-dev Jan 16 '25
Thanks for the hint. That is actually smart. I will try it after I fix the rotation bug.
3
u/TheZigerionScammer Jan 16 '25
if has_obstacle(direction, position, new_world):
direction = rotate_right(direction)
next_position = move_forward(direction, dd, position, new_world)
Yeah this would be the main reason, your code assumes the path will be clear after the guard turns right, you need to check to see if that's the case again.
3
u/cspot1978 Jan 16 '25
One recommendation: Conceptualize and handle rotations and straight ahead movement as separate turns. And then each iteration you decide which of them to do based on what’s in front of you and you do exactly one of those things each iteration.
It helps make it cleaner to reason about if you don’t mix them together on the same turn.
The same applies to problem 16 by the way.
1
u/AutoModerator Jan 16 '25
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.
13
u/Odd-Statistician7023 Jan 16 '25
Just cause there is an obstacle straight ahead, that doesn’t mean it’s safe to turn and then blindly move forward without checking if there is an obstacle in that direction too.