DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for AoC 2015 - Day 12 - More RegEx!
Jules
Jules

Posted on

AoC 2015 - Day 12 - More RegEx!

We're back parsing large strings again, with the aim today of pulling numbers out of a long JSON string. Here's Part 1:

Screenshot of Advent of Code puzzle

And here's a small(!) sample of the puzzle input:

[{"a":{"e":{"e":161,"a":"blue","d":{"e":-14,"a":"red","d":{"c":"yellow","a":[-35,0],"b":"orange","d":{"e":70,"a":"green","d":"blue","j":12,"c":69,"h":"orange","b":92,"g":"yellow","f":"green","i":121}},"c":"blue","h":14,"b":46,"g":62,"f":[179]},"j":{"e":133,"c":"violet","a":"orange","b":"blue","d":"violet"},"c":{"e":151,"a":"violet","d":{"e":"red","c":93,"a":135,"g":{"e":43,"c":"green","a":"orange","b":"green","d":54},"b":69,"d":159,"f":2},"c":"green","h":65,"b":{"c":126,"a":106,"b":190,"d":-40},"g":134,"f":"red"},"h":87,"b":[-3,"yellow",50,120],"g":{"e":[138,83,"red"],"c":["yellow",["red",177,98,"blue",179,"blue","violet",23],108,["green",17,-46,3,99]
Enter fullscreen mode Exit fullscreen mode

It seems really wasteful to step through the string when there's a perfectly good alternative in RegEx, regardless of how terrible at regular expressions I am. So it's back to RegExr with a sample of the puzzle input. Fortunately, the RegEx is fairly straightforward:

-?\d+

This roughly translates to "zero or more minuses, followed by one or more digits", which you can see explained here (with some sample input).

This means that the solution to Part 1 can be done in a single line of code, though I'm not claiming this as golfing:

print(sum(int(i) for i in re.findall(r'-?\d+', str(data))))
Enter fullscreen mode Exit fullscreen mode

Part 2 involves us actually having to parse the data:

Screenshot of Advent of Code puzzle

Thankfully, Python provides a JSON parser as part of its standard library, and it has no trouble loading the puzzle input into a JSON document:

import json
with open('txt/day12.txt') as f:
    data = json.loads(f.read())
Enter fullscreen mode Exit fullscreen mode

In simplest terms, this turns the data into a structured set of nodes, which we can travel through using recursion. Any nodes containing the banned word 'red' can be ignored, and any numbers get added to the sum:

def sum_numbers(node):

    if type(node) is int:
        return node

    if type(node) is list:
        return sum(map(sum_numbers, node))

    if type(node) is dict:
        vals = node.values()

        if "red" in vals:
            return 0

        return sum(map(sum_numbers, vals))

    else:

        return 0

print(sum_numbers(data))
Enter fullscreen mode Exit fullscreen mode

This runs surprisingly quickly, and I don't remember seeing any surprising variations in the Reddit megathread. However, despite my best efforts, I have had to resort to regular expressions enough times to think I should spend a bit of time learning it properly. Also, maybe I should put together a little post of handy bits of RegEx, especially where they might be helpful in Advent of Code? Let me know what you think.

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: