From 8f06308e1d705d61db7e843f783b42991eea0fe4 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Tue, 10 Dec 2024 19:49:31 +0100 Subject: [PATCH] Solved 2015/12 P1 --- 2015/12/12.md | 50 +++++++++++++++++++++++++++++++++++++++++++++ 2015/12/solution.py | 32 +++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 2015/12/12.md create mode 100644 2015/12/solution.py diff --git a/2015/12/12.md b/2015/12/12.md new file mode 100644 index 0000000..a435c7c --- /dev/null +++ b/2015/12/12.md @@ -0,0 +1,50 @@ +## \-\-- 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](http://json.org/) 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 `191164`. + +The first half of this puzzle is complete! It provides one gold star: \* + +## \-\-- Part Two \-\-- {#part2} + +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. + +Answer: + +Although it hasn\'t changed, you can still [get your puzzle +input](12/input). + diff --git a/2015/12/solution.py b/2015/12/solution.py new file mode 100644 index 0000000..ccff440 --- /dev/null +++ b/2015/12/solution.py @@ -0,0 +1,32 @@ +#!/bin/python3 +import sys,time,re +from pprint import pprint +sys.path.insert(0, '../../') +from fred import list2int,get_re,nprint,lprint,loadFile +start_time = time.time() + +input_f = 'input' + +part = 1 +######################################### +# # +# Part 1 # +# # +######################################### + +if part == 1: + with open(input_f) as file: + instructions = file.readline().rstrip() + + match = re.findall(r"(-?\d+)",instructions) + print(sum(list2int(match))) + +######################################### +# # +# Part 2 # +# # +######################################### +if part == 2: + exit() + +print("--- %s seconds ---" % (time.time() - start_time)) \ No newline at end of file