2024-11-22 18:03:02 +01:00
|
|
|
#!/bin/python3
|
|
|
|
import sys,re
|
|
|
|
from pprint import pprint
|
|
|
|
|
2024-11-22 21:40:18 +01:00
|
|
|
input_f = 'input'
|
2024-11-22 18:03:02 +01:00
|
|
|
|
2024-11-22 21:55:01 +01:00
|
|
|
part = 2
|
2024-11-22 18:03:02 +01:00
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 1 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
if part == 1:
|
2024-11-22 21:40:18 +01:00
|
|
|
with open(input_f) as file:
|
2024-11-22 18:03:02 +01:00
|
|
|
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)
|
2024-11-22 21:40:18 +01:00
|
|
|
level = 0
|
|
|
|
numbers = []
|
|
|
|
for i in clean_string:
|
|
|
|
if i == '{':
|
|
|
|
level += 1
|
|
|
|
if i == '}':
|
|
|
|
numbers.append(level)
|
|
|
|
level -= 1
|
2024-11-22 18:03:02 +01:00
|
|
|
|
2024-11-22 21:40:18 +01:00
|
|
|
print(sum(numbers))
|
2024-11-22 18:03:02 +01:00
|
|
|
|
|
|
|
#########################################
|
|
|
|
# #
|
2024-11-22 21:55:01 +01:00
|
|
|
# not working Part 2 #
|
2024-11-22 18:03:02 +01:00
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
if part == 2:
|
2024-11-22 21:55:01 +01:00
|
|
|
with open(input_f) as file:
|
|
|
|
for line in file:
|
2024-11-22 22:09:02 +01:00
|
|
|
#print(line.rstrip(), end=' ')
|
|
|
|
length = 0
|
2024-11-22 21:55:01 +01:00
|
|
|
clean_string = line.rstrip()
|
|
|
|
clean_string = re.sub(r"!.",'',clean_string)
|
2024-11-22 22:09:02 +01:00
|
|
|
clean_string = re.findall(r"<(.*?)>", clean_string)
|
|
|
|
|
|
|
|
for i in clean_string:
|
|
|
|
length += len(i)
|
|
|
|
print(length)
|