r/adventofcode • • Dec 13 '17

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

--- Day 13: Packet Scanners ---


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!

16 Upvotes

199 comments sorted by

View all comments

Show parent comment

2

u/ludic Dec 13 '17

You can use

for key, item in dict.items():

instead of

for key in dict.keys():
   dict[keys] ...

So your severity function would be

def severity(lengths):
    total = 0
    for key, length in lengths.items():
        if key % (2 * (length - 1)) == 0:
            total += length * key
    return total

Saves a little typing and an additional dictionary lookup.

1

u/maxerickson Dec 13 '17

And then the next step is to store the pairs in a list. In my solution, sorted by length is ~20% faster than the order from the input.

1

u/tobiasvl Dec 14 '17

Aaaah, because there's a bigger chance of getting detected in more shallow layers because the scanners bounce more frequently?

1

u/Cole_from_SE Dec 13 '17

Ah, good point. I'm not sure why I didn't think to do that or tuples. Guess you just do what's most natural for you when under time pressure (I never think to do list/dict comprehensions either).