You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
975 B
37 lines
975 B
import json
|
|
|
|
file = open('input.txt', 'r')
|
|
decoded = json.loads(file.read())
|
|
#decoded = json.loads('{"a":{"b":4},"c":-1}')
|
|
#decoded = json.loads('[1,{"c":"red","b":2},3]')
|
|
|
|
total = 0
|
|
|
|
def contains_red(pIterable):
|
|
contains_red = False
|
|
for k, v in pIterable.items():
|
|
if isinstance(v, str):
|
|
if v == "red":
|
|
contains_red = True
|
|
return contains_red
|
|
|
|
def recurse(pIterable):
|
|
global total
|
|
if isinstance(pIterable, dict):
|
|
if contains_red(pIterable):
|
|
return
|
|
for k, v in pIterable.items():
|
|
if isinstance(v, list) or isinstance(v, dict):
|
|
recurse(v)
|
|
elif isinstance(v, int):
|
|
total += int(v)
|
|
|
|
elif isinstance(pIterable, list):
|
|
for a in pIterable:
|
|
if isinstance(a, list) or isinstance(a, dict):
|
|
recurse(a)
|
|
elif isinstance(a, int):
|
|
total += int(a)
|
|
|
|
recurse(decoded)
|
|
print(total)
|
|
|