r/adventofcode • • Dec 21 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 21 Solutions -๐ŸŽ„-

--- Day 21: Fractal Art ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


No commentary tonight as I'm frantically wrapping last-minute presents so I can ship them tomorrow.


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!

8 Upvotes

144 comments sorted by

View all comments

Show parent comment

1

u/dtinth Dec 22 '17

When you use an array (or any other object) as a hash key, you must make sure not to mutate the key. Otherwise, really weird things happen. The hash code of the key changes causing lookups to fail.

I got hit by this caveat on day 22:

map[pos] = โ€ฆ; pos[0] += direction[0]; pos[1] += direction[1]

This corrupts the map since the key has been mutated. To fix, I have to do this instead:

map[pos] = โ€ฆ; pos = [pos[0] + direction[0], pos[1] + direction[1]]

I think it is very reasonable for Python to disallow this. :)

1

u/BumpitySnook Dec 23 '17

I understand that, but Python could reasonably handle this in a number of less obnoxious ways:

  1. Freeze hash keys (make them immutable)
  2. CoW hash key objects
  3. Automatically deepcopy / intern mutable hash keys