Description
Santa's Accounting-Elves need help balancing the books after a recent order. Unfortunately, their accounting software uses a peculiar storage format. That's where you come in.
They have a JSON document which contains a variety of things: arrays ([1,2,3]), objects ({"a":1, "b":2}), numbers, and strings. Your first job is to simply find all of the numbers throughout the document and add them together.
For example:
[1,2,3]and{"a":2,"b":4}both have a sum of6.[[[3]]]and{"a":{"b":4},"c":-1}both have a sum of3.{"a":[-1,1]}and[-1,{"a":1}]both have a sum of0.[]and{}both have a sum of0.
You will not encounter any strings containing numbers.
What is the sum of all numbers in the document?
--- Part Two ---
Uh oh - the Accounting-Elves have realized that they double-counted everything red.
Ignore any object (and all of its children) which has any property with the value "red". Do this only for objects ({...}), not arrays ([...]).
[1,2,3]still has a sum of6.[1,{"c":"red","b":2},3]now has a sum of4, because the middle object is ignored.{"d":"red","e":[1,2,3,4],"f":5}now has a sum of0, because the entire structure is ignored.[1,"red",5]has a sum of6, because"red"in an array has no effect.
Notes
- Oh boy, JSON decoding
- I've tried several different methods to try to iterate through JSON in python, but cannot find one that fits my need
- Most of my time is just spent on parsing data and correctly accessing lists and dictionaries correctly
- In the end, I just wrote my own parser, and it was actually quite simple once I realized it's either a list or a dictionary or an integer, and recursed if I needed to go deeper
- 12-2 was easy peasy because of all the time I spent figuring out the best way to parse the data