Description Usage Arguments Details Examples
1 2 3 | process_stream(stream)
count_garbage(stream)
|
stream |
a string to process |
Part One
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 \code{>}.
}
Here are some examples of whole streams and the number of groups they
contain:
\itemize{
\item \code{\{\}}, \code{1} group.
\item \code{\{\{\{\}\}\}}, \code{3} groups.
\item \code{\{\{\},\{\}\}}, also \code{3} groups.
\item \code{\{\{\{\},\{\},\{\{\}\}\}\}}, \code{6} groups.
\item \code{\{<\{\},\{\},\{\{\}\}>\}}, \code{1} group (which itself contains
garbage).
\item \code{\{<a>,<a>,<a>,<a>\}}, \code{1} group.
\item \code{\{\{<a>\},\{<a>\},\{<a>\},\{<a>\}\}}, \code{5} groups.
\item \code{\{\{<!>\},\{<!>\},\{<!>\},\{<a>\}\}}, \code{2} groups
(since all but the last \code{>} are canceled).
}
Your goal is to find the total score for all groups in your input. Each
group is assigned a \emph{score} which is one more than the score of the
group that immediately contains it. (The outermost group gets a score of
\code{1}.)
\itemize{
\item \code{\{\}}, score of \code{1}.
\item \code{\{\{\{\}\}\}}, score of \code{1 + 2 + 3 = 6}.
\item \code{\{\{\},\{\}\}}, score of \code{1 + 2 + 2 = 5}.
\item \code{\{\{\{\},\{\},\{\{\}\}\}\}}, score of
\code{1 + 2 + 3 + 3 + 3 + 4 = 16}.
\item \code{\{<a>,<a>,<a>,<a>\}}, score of \code{1}.
\item \code{\{\{<ab>\},\{<ab>\},\{<ab>\},\{<ab>\}\}},
score of \code{1 + 2 + 2 + 2 + 2 = 9}.
\item \code{\{\{<!!>\},\{<!!>\},\{<!!>\},\{<!!>\}\}},
score of \code{1 + 2 + 2 + 2 + 2 = 9}.
\item \code{\{\{<a!>\},\{<a!>\},\{<a!>\},\{<ab>\}\}}, score of \code{1 + 2 = 3}.
}
\emph{What is the total score} for all groups in your input?
\strong{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 \code{<} and \code{>} don't count, nor
do any canceled characters or the \code{!} doing the canceling.
\itemize{
\item \code{<>}, \code{0} characters.
\item \code{<random characters>}, \code{17} characters.
\item \code{<<<<>}, \code{3} characters.
\item \code{<\{!>\}>}, \code{2} characters.
\item \code{<!!>}, \code{0} characters.
\item \code{<!!!>>}, \code{0} characters.
\item \code{<\{o"i!a,<{i<a>
, 10
characters.
How many non-canceled characters are within the garbage in your puzzle input?
1 2 | process_stream("{{<ab>},{<ab>},{<ab>},{<ab>}}")
count_garbage("{{<ab>},{<ab>},{<ab>},{<ab>}}")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.