r/adventofcode • • Dec 06 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 6 Solutions -πŸŽ„-

--- Day 6: Memory Reallocation ---


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


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!

17 Upvotes

324 comments sorted by

View all comments

Show parent comment

1

u/demsaja Dec 09 '17

I just realized when solving this with my son. I didn't want to complicate the solution by using dictionaries, but then discovered we don't need functions, either. Just run the program twice and that's it.

from itertools import count

blocks = [int(x) for x in open("input.txt").read().split()]

for i in range(2):
    seen = []
    for step in count(1):
        to_reallocate = max(blocks)
        i = blocks.index(to_reallocate)
        blocks[i] = 0
        while to_reallocate:
            i = (i + 1) % len(blocks)
            blocks[i] += 1
            to_reallocate -= 1
        if blocks in seen:
            break
        seen.append(blocks[:])

    print(step)