diff --git a/2015/12/12.md b/2015/12/12.md index a435c7c..e6a3c42 100644 --- a/2015/12/12.md +++ b/2015/12/12.md @@ -24,8 +24,6 @@ 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 @@ -43,8 +41,14 @@ the value `"red"`. Do this only for objects (`{...}`), not arrays - `[1,"red",5]` has a sum of `6`, because `"red"` in an array has no effect. -Answer: +Your puzzle answer was `87842`. -Although it hasn\'t changed, you can still [get your puzzle +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2015) and +try another puzzle. + +If you still want to see it, you can [get your puzzle input](12/input). diff --git a/2015/12/solution.py b/2015/12/solution.py index ccff440..f96727f 100644 --- a/2015/12/solution.py +++ b/2015/12/solution.py @@ -1,5 +1,5 @@ #!/bin/python3 -import sys,time,re +import sys,time,re,json from pprint import pprint sys.path.insert(0, '../../') from fred import list2int,get_re,nprint,lprint,loadFile @@ -7,26 +7,56 @@ start_time = time.time() input_f = 'input' -part = 1 ######################################### # # # Part 1 # # # ######################################### -if part == 1: +def loadLine(input_f:str) -> str: with open(input_f) as file: instructions = file.readline().rstrip() - - match = re.findall(r"(-?\d+)",instructions) - print(sum(list2int(match))) + return instructions + +def sum_numbers(inst:str) -> int: + return sum(list2int(re.findall(r"(-?\d+)",inst))) + +def part1() -> int: + return sum_numbers(loadLine(input_f)) + +start_time = time.time() +print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') ######################################### # # # Part 2 # # # ######################################### -if part == 2: - exit() -print("--- %s seconds ---" % (time.time() - start_time)) \ No newline at end of file +def remove_red(data): + if isinstance(data, dict): + if "red" in data.values(): + return None + + filtered_dict = {} + for key, value in data.items(): + filtered_value = remove_red(value) + if filtered_value is not None: + filtered_dict[key] = filtered_value + return filtered_dict + + elif isinstance(data, list): + filtered_list = [] + for item in data: + filtered_item = remove_red(item) + if filtered_item is not None: + filtered_list.append(filtered_item) + return filtered_list + else: + return data + +def part2(): + return sum_numbers(str(remove_red(json.loads(loadLine(input_f))))) + +start_time = time.time() +print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms') diff --git a/__pycache__/fred.cpython-311.pyc b/__pycache__/fred.cpython-311.pyc index af47874..f6fa3e8 100644 Binary files a/__pycache__/fred.cpython-311.pyc and b/__pycache__/fred.cpython-311.pyc differ diff --git a/solution.py b/solution.py index 7ec8d9e..ccfa218 100644 --- a/solution.py +++ b/solution.py @@ -7,14 +7,16 @@ start_time = time.time() input_f = 'test' -part = 1 ######################################### # # # Part 1 # # # ######################################### - -if part == 1: +def part1(): + return + +start_time = time.time() +print('Part 2:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') ######################################### @@ -22,7 +24,8 @@ if part == 1: # Part 2 # # # ######################################### -if part == 2: - exit() - -print("--- %s seconds ---" % (time.time() - start_time)) \ No newline at end of file +def part1(): + return + +start_time = time.time() +print('Part 2:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') \ No newline at end of file