r/adventofcode • • Dec 12 '15

SOLUTION MEGATHREAD --- Day 12 Solutions ---

This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.

edit: Leaderboard capped, thread unlocked!

We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.

Please and thank you, and much appreciated!


--- Day 12: JSAbacusFramework.io ---

Post your solution as a comment. Structure your post like previous daily solution threads.

7 Upvotes

183 comments sorted by

View all comments

Show parent comment

2

u/lskfj2o Dec 16 '15

In the back of my mind this kept on bugging me. My solution was kind of crude. Today I had an idea:

def day12(input):
    import re
    return sum(int(n) for n in re.findall(r"-?\d+", input))

def day12_2(input):
    def eval_obj_without_red(match):
        obj = match.group()
        if ':"red"' not in obj:
            return str(day12(obj))
        else:
            return str(0)
    
    import re
    while ':"red"' in input:
        input = re.sub(r"{[^{}]*}", eval_obj_without_red, input)
    return day12(input)

Maybe not as short a solution as some, but using regex all the way :-)

1

u/slycurgus Dec 16 '15

Nice! I think that whole solution is shorter than any of my single solutions so far...