Solutions for 2022, as well as 2015-2018 and 2019 up to day 11

This commit is contained in:
Chris Alge 2023-03-12 15:20:02 +01:00
commit 1895197c49
722 changed files with 375457 additions and 0 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "day09-stream_processing"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,73 @@
\--- Day 9: Stream Processing ---
----------
A large stream blocks your path. According to the locals, it's not safe to cross the stream 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.
* `<random characters>`, 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<a>`, 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).
* `{<a>,<a>,<a>,<a>}`, `1` group.
* `{{<a>},{<a>},{<a>},{<a>}}`, `5` groups.
* `{{<!>},{<!>},{<!>},{<a>}}`, `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`.
* `{<a>,<a>,<a>,<a>}`, score of `1`.
* `{{<ab>},{<ab>},{<ab>},{<ab>}}`, score of `1 + 2 + 2 + 2 + 2 = 9`.
* `{{<!!>},{<!!>},{<!!>},{<!!>}}`, score of `1 + 2 + 2 + 2 + 2 = 9`.
* `{{<a!>},{<a!>},{<a!>},{<ab>}}`, score of `1 + 2 = 3`.
*What is the total score* for all groups in your input?
Your puzzle answer was `8337`.
\--- Part Two ---
----------
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?
Your puzzle answer was `4330`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, all that is left is for you to [admire your Advent calendar](/2017).
If you still want to see it, you can [get your puzzle input](9/input).

View file

@ -0,0 +1,47 @@
pub fn run(stream: &str) -> (usize, usize) {
let mut garbage = false;
let mut ignore_next = false;
let mut score = 0;
let mut garbage_count = 0;
let mut current_depth = 0;
stream.chars().for_each(|c| {
if ignore_next {
ignore_next = false;
} else if garbage {
match c {
'!' => ignore_next = true,
'>' => garbage = false,
_ => garbage_count += 1,
}
} else {
match c {
'<' => garbage = true,
'{' => {
current_depth += 1;
score += current_depth;
},
'}' => current_depth -= 1,
_ => (),
}
}
});
(score, garbage_count)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), (8337, 4330));
}
}

File diff suppressed because one or more lines are too long