AdventOfCode/2015/12/solution.py

63 lines
1.8 KiB
Python

#!/bin/python3
import sys,time,re,json
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 #
# #
#########################################
def loadLine(input_f:str) -> str:
with open(input_f) as file:
instructions = file.readline().rstrip()
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 #
# #
#########################################
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')