2024-12-10 19:49:31 +01:00
|
|
|
#!/bin/python3
|
2024-12-11 22:48:21 +01:00
|
|
|
import sys,time,re,json
|
2024-12-10 19:49:31 +01:00
|
|
|
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 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
|
2024-12-11 22:48:21 +01:00
|
|
|
def loadLine(input_f:str) -> str:
|
2024-12-10 19:49:31 +01:00
|
|
|
with open(input_f) as file:
|
|
|
|
instructions = file.readline().rstrip()
|
2024-12-11 22:48:21 +01:00
|
|
|
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')
|
2024-12-10 19:49:31 +01:00
|
|
|
|
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 2 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
|
2024-12-11 22:48:21 +01:00
|
|
|
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')
|