r/adventofcode • • Dec 24 '17

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

--- Day 24: Electromagnetic Moat ---


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


[Update @ 00:18] 62 gold, silver cap

  • Been watching Bright on Netflix. I dunno why reviewers are dissing it because it's actually pretty cool. It's got Will Smith being grumpy jaded old man Will Smith, for the love of FSM...

[Update @ 00:21] Leaderboard cap!

  • One more day to go in Advent of Code 2017... y'all ready to see Santa?

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

9 Upvotes

108 comments sorted by

View all comments

Show parent comment

1

u/u794575248 Dec 24 '17 edited Dec 24 '17

And an optimized one using set instead of a list for bridge components:

def gen_bridges(library, bridge=None):
    l, s, components, a = bridge or (0, 0, set(), 0)
    for b in library[a]:
        next = (a, b) if a <= b else (b, a)
        if next not in components:
            new = l+1, s+a+b, (components | {next}), b
            yield new; yield from gen_bridges(library, new)

def solve(input):
    library = defaultdict(set)
    for l in input.strip().splitlines():
        a, b = [int(x) for x in l.split('/')]
        library[a].add(b); library[b].add(a)
    return [b[:2] for b in gen_bridges(library)]

bridges = solve(input)  # A list of (length, strength) tuples
part1 = sorted(bridges, key=lambda x: x[1])[-1][1]  # Sort by strength only
part2 = sorted(bridges)[-1][1]  # Sort by length, then by strength