#!/bin/python3 import sys,re from pprint import pprint input_f = 'input' part = 2 ######################################### # # # Part 1 # # # ######################################### if part == 1: with open(input_f) as file: for line in file: clean_string = re.sub(r"!.", "", line.rstrip()) clean_string = re.sub(r"<.*?>", "", clean_string) clean_string = re.sub(r",", "", clean_string) level = 0 numbers = [] for i in clean_string: if i == '{': level += 1 if i == '}': numbers.append(level) level -= 1 print(sum(numbers)) ######################################### # # # Part 2 # # # ######################################### if part == 2: with open(input_f) as file: for line in file: #print(line.rstrip(), end=' ') length = 0 clean_string = line.rstrip() clean_string = re.sub(r"!.",'',clean_string) clean_string = re.findall(r"<(.*?)>", clean_string) for i in clean_string: length += len(i) print(length)