Solved 2015/12 P1+P2

This commit is contained in:
FrederikBaerentsen 2024-12-11 22:48:21 +01:00
parent 9e95a2d75f
commit 41a0ed3e8e
4 changed files with 57 additions and 20 deletions

View File

@ -24,8 +24,6 @@ What is the *sum of all numbers* in the document?
Your puzzle answer was `191164`. Your puzzle answer was `191164`.
The first half of this puzzle is complete! It provides one gold star: \*
## \-\-- Part Two \-\-- {#part2} ## \-\-- Part Two \-\-- {#part2}
Uh oh - the Accounting-Elves have realized that they double-counted 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 - `[1,"red",5]` has a sum of `6`, because `"red"` in an array has no
effect. 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). input](12/input).

View File

@ -1,5 +1,5 @@
#!/bin/python3 #!/bin/python3
import sys,time,re import sys,time,re,json
from pprint import pprint from pprint import pprint
sys.path.insert(0, '../../') sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile from fred import list2int,get_re,nprint,lprint,loadFile
@ -7,26 +7,56 @@ start_time = time.time()
input_f = 'input' input_f = 'input'
part = 1
######################################### #########################################
# # # #
# Part 1 # # Part 1 #
# # # #
######################################### #########################################
if part == 1: def loadLine(input_f:str) -> str:
with open(input_f) as file: with open(input_f) as file:
instructions = file.readline().rstrip() instructions = file.readline().rstrip()
return instructions
match = re.findall(r"(-?\d+)",instructions) def sum_numbers(inst:str) -> int:
print(sum(list2int(match))) 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 # # Part 2 #
# # # #
######################################### #########################################
if part == 2:
exit()
print("--- %s seconds ---" % (time.time() - start_time)) 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')

Binary file not shown.

View File

@ -7,14 +7,16 @@ start_time = time.time()
input_f = 'test' input_f = 'test'
part = 1
######################################### #########################################
# # # #
# Part 1 # # Part 1 #
# # # #
######################################### #########################################
def part1():
return
if part == 1: 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 # # Part 2 #
# # # #
######################################### #########################################
if part == 2: def part1():
exit() return
print("--- %s seconds ---" % (time.time() - start_time)) start_time = time.time()
print('Part 2:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')