Advent of Code 2015 day 12 part 1&2: solved

This commit is contained in:
Fabian Tessmer 2025-03-24 12:00:32 +01:00
parent d6d81fc95c
commit d6bccb2e1f
3 changed files with 4471 additions and 0 deletions

4389
2015/day12/input.json Normal file

File diff suppressed because it is too large Load Diff

29
2015/day12/problem.txt Normal file
View File

@ -0,0 +1,29 @@
--- Day 12: JSAbacusFramework.io ---
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 of 6.
[[[3]]] and {"a":{"b":4},"c":-1} both have a sum of 3.
{"a":[-1,1]} and [-1,{"a":1}] both have a sum of 0.
[] and {} both have a sum of 0.
You will not encounter any strings containing numbers.
What is the sum of all numbers in the document?
Your puzzle answer was 111754.
--- 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 of 6.
[1,{"c":"red","b":2},3] now has a sum of 4, because the middle object is ignored.
{"d":"red","e":[1,2,3,4],"f":5} now has a sum of 0, because the entire structure is ignored.
[1,"red",5] has a sum of 6, because "red" in an array has no effect.
Your puzzle answer was 65402.
Both parts of this puzzle are complete! They provide two gold stars: **

53
2015/day12/solution.py Normal file
View File

@ -0,0 +1,53 @@
import json
def get_sum(json_input: dict | list) -> int:
total = 0
if type(json_input) is list:
for i in json_input:
total += get_sum(i)
elif type(json_input) is dict:
for key in json_input:
value = json_input[key]
total += get_sum(value)
elif type(json_input) is int:
total += json_input
return total
def get_sum_ignore_red(json_input: dict | list) -> int:
total = 0
if type(json_input) is list:
for i in json_input:
total += get_sum_ignore_red(i)
elif type(json_input) is dict:
mini_total, contains_red = 0, False
for key in json_input:
value = json_input[key]
mini_total += get_sum_ignore_red(value)
if value == "red":
contains_red = True
if not contains_red:
total += mini_total
elif type(json_input) is int:
total += json_input
return total
if __name__ == '__main__':
print(get_sum([1, 2, 3]))
assert get_sum([1, 2, 3]) == 6, "Error: Example 1 failed"
assert get_sum({"a": 2, "b": 4}) == 6, "Error: Example 2 failed"
assert get_sum([[[3]]]) == 3, "Error: Example 3 failed"
assert get_sum({"a": {"b": 4}, "c": -1}) == 3, "Error: Example 4 failed"
assert get_sum({"a": [-1, 1]}) == 0, "Error: Example 5 failed"
assert get_sum([-1, {"a": 1}]) == 0, "Error: Example 6 failed"
assert get_sum([]) == 0, "Error: Example 7 failed"
assert get_sum({}) == 0, "Error: Example 8 failed"
print("All test passed")
puzzle_json = json.load(fp=open('input.json', "r"))
print("solution: ", get_sum(puzzle_json))
print("Part2: ")
print("solution: ", get_sum_ignore_red(puzzle_json))