r/adventofcode • • Dec 22 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 22 Solutions -🎄-

--- Day 22: Mode Maze ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 22

Transcript:

Upping the Ante challenge: complete today's puzzles using ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 01:02:36!

12 Upvotes

102 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Jan 23 '19 edited Jan 23 '19

I think your problem is, is that you can't necessarily change tool and move. I've not extensively debugged your code though so I may have made a mistake and read your code incorrectly.

From the puzzle description "You can change your currently equipped tool or put both away if your new equipment would be valid for your current region"

i.e to move you need to be carrying a tool that is allowed in the new square. But, to change tool, you need to change to a tool (or none) that is allowed in the current square.

e.g You can't change and move for 8 from a water square carrying nothing to a rocky carrying the torch, because that is actually 2 moves, one from nothing->torch - which isn't allowed in the water region, followed by a move to the rocky square.

I think you may have found a shorter path but only because it breaks the above rule - you are switching tools and moving in one step but only heeding what tools are allowed in the new square.

I'm also puzzling about some of your assumptions about the paths. I think it may be possible to go past the target and backtrack because each move isn't equal. i.e you can do 7 moves in the time of 1 tool change, so it may be possible the shortest time goes past the target. You seem to be only checking tx >= dx >= 0 and similarly for dy and never moving y - 1. Although changing these doesn't make your code give the right answer so there's another bug.

I would say, get your program to print out the path it's generating and then compare that path with someone else's code that shows the path (I believe my code here gets the correct value for your input) It'll be easier to see the difference than seeing 1064 and 1070.

Lastly, just a nitpick, you have geo_index memoized but if you switch to functools lru_cache and call lru_cache.cache_info() you'll note that it'll never get any cache hits and therefore there's no benefit to caching it. erosion_level is the only function of the pair that needs the memoization for the speed up.

erosion_level.cache_info()
Out[6]: CacheInfo(hits=1067077, misses=116537, maxsize=None, currsize=116537)

geo_index.cache_info()
Out[7]: CacheInfo(hits=0, misses=116537, maxsize=None, currsize=116537)

2

u/EugeniuZZZ Jan 23 '19

Thank you for your feedback. It helped me fix the program.

I indeed misunderstood the puzzle input that you mention. I was changing the tools to the one suitable for next zone while being in the current zone without considering that it is forbidden to use it in the current zone. You're also right about not needing to memoize the geo_index function.

About your other comment I have to clarify:

  • I have 2 functions for computing the shortest path: _find_path_in_bounding_box and solution2. solution2 is the actual function.
  • in solution2 I am using a variable min_path_time that I use to trim partial paths that would result in longer complete paths than the currently shortest path (that is min_path_time).
  • one can use Manhattan distance between origin and target and multiply it by 8 to get an upper bound on the shortest path (so we assume the worst case when we need to change tools every time)
  • to improve on this naive estimate I use _find_path_in_bounding_box to find a path that doesn't go outside the bounding box formed by origin and target. Also I don't go back to speed up the computation of this initially best path.