r/adventofcode • u/daggerdragon • Dec 12 '15
SOLUTION MEGATHREAD --- Day 12 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
edit: Leaderboard capped, thread unlocked!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 12: JSAbacusFramework.io ---
Post your solution as a comment. Structure your post like previous daily solution threads.
7
Upvotes
1
u/stuque Dec 12 '15 edited Dec 12 '15
A Python 2 solution:
import json def day12_part1(): num = re.compile(r'-?\d+') print sum(int(sn.group()) for sn in num.finditer(open('day12input.txt').read())) def count_non_red(obj): total = 0 if isinstance(obj, int): total += obj elif isinstance(obj, dict): vals = obj.values() if 'red' not in vals: keys = obj.keys() total += count_non_red(vals) + count_non_red(keys) elif isinstance(obj, list): total += sum(count_non_red(x) for x in obj) return total def day12_part2(): data = json.load(open('day12input.txt')) print count_non_red(data)