From 41a0ed3e8ecddd2aaa32a9a2eb7c3a6e934b55ad Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Wed, 11 Dec 2024 22:48:21 +0100 Subject: [PATCH] Solved 2015/12 P1+P2 --- 2015/12/12.md | 12 +++++--- 2015/12/solution.py | 48 +++++++++++++++++++++++++------ __pycache__/fred.cpython-311.pyc | Bin 25353 -> 25507 bytes solution.py | 17 ++++++----- 4 files changed, 57 insertions(+), 20 deletions(-) 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 af478742f1b89a543d196447415f811cc6f9fcfb..f6fa3e8c5e23203571b37688532b8953434d5713 100644 GIT binary patch delta 2177 zcmah~Z%k8H6o0p`uN1LmI>5GId94L3N|6o(v7&(DU|Ug#fI69lK0$?+ot7~TY}0*V zAEsk>iTk6D%x%s&H}UD*vc*MM^n;nf#U-=LvN@A|G=9)znkDX>TZl@Q`I7$L@BQw% z=ic+@-oAdG9lpS663?o8VNgF*w^#Z$>o@)@l^>p}s+eA{mgI~>TXGMA- z>u;7>LSqI3Kfx0OWxU>&ZC`?PkUq-^iU`~Y9gJ6JW$44UdyM({u*1gO{4>W=c8LGw zs9;4rH#-NFK(<3!f!6!@rtC&s>sa1bCpIk9MRv* zJvw?bPD6S|qLkgi*cDo4rxo z!g+7d7!>6oZwcurOUPOze3z--6qth&loF8pgdB@QB2|m@^=J@6MT@P2- z96gLEC=v2?^F zD(ly01r+oDH^jY_KHIMU$OttApH;ePA+-~BwJ06+19H)_N=Gyb;c4xK-q_3Bw~C?( zuWGp82sn#!3`0o|f>W2EPSTVv_@pkHXwqJAKmTVHxCFJ)9Fg}fa+3OB<@mlj)%+C6 zR;=2YI#=3jWIcJ2T(1i$@8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1srtM{%Mm)L}JW^c5KJs=rYewGeOGcy9o!iA~J6&*hr>Edq zw$2VI+}GK!ze?{h^T*EfD%IDCu3t22ZVasum+-5QK_~w+)KtnO-&5dqdd%sdH*sCq zBR;{$!j(DPJs~)VFNGUyD@o4isdPW2VGyUnFY0GVbH{wu=5K7Rp5E6*+f 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