Aded 2017/09 part 1

This commit is contained in:
FrederikBaerentsen 2024-11-22 21:40:18 +01:00
parent 532ca84519
commit 7a29670437
2 changed files with 37 additions and 30 deletions

View File

@ -70,6 +70,30 @@ group that immediately contains it. (The outermost group gets a score of
*What is the total score* for all groups in your input? *What is the total score* for all groups in your input?
To begin, [get your puzzle input](9/input). Your puzzle answer was `11898`.
The first half of this puzzle is complete! It provides one gold star: \*
## \-\-- Part Two \-\-- {#part2}
Now, you\'re ready to remove the garbage.
To prove you\'ve removed it, you need to count all of the characters
within the garbage. The leading and trailing `<` and `>` don\'t count,
nor do any canceled characters or the `!` doing the canceling.
- `<>`, `0` characters.
- `<random characters>`, `17` characters.
- `<<<<>`, `3` characters.
- `<{!>}>`, `2` characters.
- `<!!>`, `0` characters.
- `<!!!>>`, `0` characters.
- `<{o"i!a,<{i<a>`, `10` characters.
*How many non-canceled characters are within the garbage* in your puzzle
input?
Answer: Answer:
Although it hasn\'t changed, you can still [get your puzzle
input](9/input).

View File

@ -2,48 +2,31 @@
import sys,re import sys,re
from pprint import pprint from pprint import pprint
input_f = 'test' input_f = 'input'
part = 1 part = -1
######################################### #########################################
# # # #
# Part 1 # # Part 1 #
# # # #
######################################### #########################################
if part == 1: if part == 1:
with open(input_f) as file: with open(input_f) as file:
for line in file: for line in file:
print(line.rstrip())
clean_string = re.sub(r"!.", "", line.rstrip()) clean_string = re.sub(r"!.", "", line.rstrip())
clean_string = re.sub(r"<.*?>", "", clean_string) clean_string = re.sub(r"<.*?>", "", clean_string)
clean_string = re.sub(r",", "", clean_string) clean_string = re.sub(r",", "", clean_string)
#print(clean_string) level = 0
numbers = []
for i in clean_string:
if i == '{':
level += 1
if i == '}':
numbers.append(level)
level -= 1
count = 1 print(sum(numbers))
score = 0
iteration = 0
#print('Score of ',end='')
while True:
#print('Iteration: ', iteration)
matches = re.findall(r"\{\}", clean_string)
if not matches:
#print(increment,end='')
print('=',score)
break
clean_string = re.sub(r"\{\}", "", clean_string)
iteration += 1
for x in range(0,len(matches)):
score += iteration
print(iteration,end="")
#print(clean_string)
#rint(count)
input()
######################################### #########################################
# # # #
# Part 2 # # Part 2 #