Advent of Code: Day 12

First puzzle

In this puzzle, we received as input a string representing a JSON document. The solution is to sum all the numbers on the input. A way to implement this is match all the numbers with a regular expression and then sum them:

Second puzzle

The second part of the puzzle is much more complicated. We need to extract the numbers and sum them in the same way as the first part, but the numbers on objects with a property value “red” needs to be ignored. Previous puzzle solution didn’t work anymore. The ignored part should be objects ({….}), not arrays ([….]). Here are some examples from the puzzle description:

It looks like we could reuse the same function on the first puzzle, but some preprocessing of its input is needed to eliminate the “red” objects. The trick is to eliminate the whole object after we find the “red” value on a property. Once we have the value position we need to look back for an object start and look ahead for the closing object. When we have those values, the object could be substituted by an empty object({}) to maintain the same structure as before but without the actual object content.

You can find this code along with my input and puzzle answers at here.

Written by Darien Martinez Torres on 12 March 2016