From 532ca84519d05b17854bf05b0b148061e2357ec4 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Fri, 22 Nov 2024 18:03:02 +0100 Subject: [PATCH] started on 2017/09 --- 2017/09/9.md | 75 +++++++++++++++++++++++++++++++++++++++++++++ 2017/09/solution.py | 53 ++++++++++++++++++++++++++++++++ solution.py | 9 ++++-- 3 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 2017/09/9.md create mode 100644 2017/09/solution.py diff --git a/2017/09/9.md b/2017/09/9.md new file mode 100644 index 0000000..b6d50dd --- /dev/null +++ b/2017/09/9.md @@ -0,0 +1,75 @@ +## \-\-- Day 9: Stream Processing \-\-- + +A large stream blocks your path. According to the locals, it\'s not safe +to [cross the +stream]{title="\"Don't cross the streams!\", they yell, even though there's only one. They seem to think they're hilarious."} +at the moment because it\'s full of *garbage*. You look down at the +stream; rather than water, you discover that it\'s a *stream of +characters*. + +You sit for a while and record part of the stream (your puzzle input). +The characters represent *groups* - sequences that begin with `{` and +end with `}`. Within a group, there are zero or more other things, +separated by commas: either another *group* or *garbage*. Since groups +can contain other groups, a `}` only closes the *most-recently-opened +unclosed group* - that is, they are nestable. Your puzzle input +represents a single, large group which itself contains many smaller +ones. + +Sometimes, instead of a group, you will find *garbage*. Garbage begins +with `<` and ends with `>`. Between those angle brackets, almost any +character can appear, including `{` and `}`. *Within* garbage, `<` has +no special meaning. + +In a futile attempt to clean up the garbage, some program has *canceled* +some of the characters within it using `!`: inside garbage, *any* +character that comes after `!` should be *ignored*, including `<`, `>`, +and even another `!`. + +You don\'t see any characters that deviate from these rules. Outside +garbage, you only find well-formed groups, and garbage always terminates +according to the rules above. + +Here are some self-contained pieces of garbage: + +- `<>`, empty garbage. +- ``, garbage containing random characters. +- `<<<<>`, because the extra `<` are ignored. +- `<{!>}>`, because the first `>` is canceled. +- ``, because the second `!` is canceled, allowing the `>` to + terminate the garbage. +- `>`, because the second `!` and the first `>` are canceled. +- `<{o"i!a,<{i`, which ends at the first `>`. + +Here are some examples of whole streams and the number of groups they +contain: + +- `{}`, `1` group. +- `{{{}}}`, `3` groups. +- `{{},{}}`, also `3` groups. +- `{{{},{},{{}}}}`, `6` groups. +- `{<{},{},{{}}>}`, `1` group (which itself contains garbage). +- `{,,,}`, `1` group. +- `{{},{},{},{}}`, `5` groups. +- `{{},{},{},{}}`, `2` groups (since all but the last `>` + are canceled). + +Your goal is to find the total score for all groups in your input. Each +group is assigned a *score* which is one more than the score of the +group that immediately contains it. (The outermost group gets a score of +`1`.) + +- `{}`, score of `1`. +- `{{{}}}`, score of `1 + 2 + 3 = 6`. +- `{{},{}}`, score of `1 + 2 + 2 = 5`. +- `{{{},{},{{}}}}`, score of `1 + 2 + 3 + 3 + 3 + 4 = 16`. +- `{,,,}`, score of `1`. +- `{{},{},{},{}}`, score of `1 + 2 + 2 + 2 + 2 = 9`. +- `{{},{},{},{}}`, score of `1 + 2 + 2 + 2 + 2 = 9`. +- `{{},{},{},{}}`, score of `1 + 2 = 3`. + +*What is the total score* for all groups in your input? + +To begin, [get your puzzle input](9/input). + +Answer: diff --git a/2017/09/solution.py b/2017/09/solution.py new file mode 100644 index 0000000..fc24504 --- /dev/null +++ b/2017/09/solution.py @@ -0,0 +1,53 @@ +#!/bin/python3 +import sys,re +from pprint import pprint + +input_f = 'test' + +part = 1 +######################################### +# # +# Part 1 # +# # +######################################### + +if part == 1: + with open(input_f) as file: + for line in file: + print(line.rstrip()) + clean_string = re.sub(r"!.", "", line.rstrip()) + clean_string = re.sub(r"<.*?>", "", clean_string) + clean_string = re.sub(r",", "", clean_string) + #print(clean_string) + + count = 1 + 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 # +# # +######################################### +if part == 2: + exit() diff --git a/solution.py b/solution.py index e9056ce..3814efb 100644 --- a/solution.py +++ b/solution.py @@ -1,16 +1,17 @@ #!/bin/python3 -import sys +import sys,re from pprint import pprint -input_f = sys.argv[1] - +input_f = 'test' +part = 1 ######################################### # # # Part 1 # # # ######################################### +if part == 1 with open(input_f) as file: for line in file: @@ -22,3 +23,5 @@ with open(input_f) as file: # Part 2 # # # ######################################### +if part == 2: + exit()