解題語言不限Java
個人感覺這個難哭了,主要還是沒干過這個
拖更了,不好意思
- Advent of Code Day 1 逆向驗證碼
- Advent of Code Day 2 損壞校驗和
- Advent of Code Day 3 螺旋內(nèi)存
- Advent of Code Day 4 高熵密碼
- Advevnt of Code Day 5 曲折的蹦床迷宮
- Advent of Code Day 6 內(nèi)存重分配
- Advent of Code Day 7 遞歸馬戲團
- Advent of Code Day 8 注冊表愛好者
- Advent of Code Day 9 流處理
- Advent of Code Day 10 結(jié)哈希
- Advent of Code Day 11 六邊形迷宮
題目內(nèi)容
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.
一個流把你的路給擋住了。根據(jù)當?shù)厝怂f,這條垃圾河是很危險的。你看著這條河,河流的不是水,是字符。
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.
你坐著等了一會并記錄下了所有流過的字符。這些字符代表著組:序列在一對中括號里{}
。在組里,有很多東西被逗號,
分開。這些組里還有組,所以每個{
會和最近的}
結(jié)合。你的謎題輸入會是大組里有很多的小組。
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.
有時,也會出現(xiàn)垃圾。垃圾是由<
開始>
結(jié)束,在這兩個符號里面,即使是{
和}
,還有<
都會被忽略。
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 !.
在你之前,已經(jīng)有些程序嘗試用!
清除一些字符。任何字符前有!
的都會被忽略,包括<
,>
,
和!
。
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.
所有的字符都會符合這個規(guī)則。在垃圾之外,所有的字符都會排列成合適的組合,并且所有的垃圾都會在符合規(guī)則的位置。
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 >.
這個垃圾在第一個>
結(jié)束。
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.)
你的目標是找出輸入里組的總分。每個組都被指定了一個分數(shù),如果這個組被3個大組包含,那分數(shù)為三。
- {}, score of 1.
分數(shù)為1 - {{{}}}, score of 1 + 2 + 3 = 6.
分數(shù)為1 - {{},{}}, score of 1 + 2 + 2 = 5.
分數(shù)為1+2+2=5 - {{{},{},{{}}}}, score of 1 + 2 + 3 + 3 + 3 + 4 = 16.
分數(shù)為1 + 2 + 3 + 3 + 3 + 4 = 16 - {<a>,<a>,<a>,<a>}, score of 1.
分數(shù)為1 - {{<ab>},{<ab>},{<ab>},{<ab>}}, score of 1 + 2 + 2 + 2 + 2 = 9.
分數(shù)為1 + 2 + 2 + 2 + 2 = 9 - {{<!!>},{<!!>},{<!!>},{<!!>}}, score of 1 + 2 + 2 + 2 + 2 = 9.
分數(shù)為1 + 2 + 2 + 2 + 2 = 9. - {{<a!>},{<a!>},{<a!>},{<ab>}}, score of 1 + 2 = 3.
分數(shù)為1 + 2 = 3
What is the total score for all groups in your input?
你的謎題輸入的總分是多少?
解題思路
萌新剛剛看到題的時候被嚇了一跳,還好我們的老油條,企鵝給了個方便的做法。
在此感謝下企鵝。
這個題目的思路主要是:
- 要能合理解析垃圾和跳躍字符
- 要對
{
和}
的層級進行正確解析
企鵝大佬的做法是
- 建一個
for
循環(huán)遍歷其中所有的字符。 - 在循環(huán)中,如果檢測到
!
就跳過一個字符 - 如果檢測到
<
,就開始忽略掉字符,直到檢測到>
。 - 定義一個層級變量和一個分數(shù)變量。每遇到一個
{
,層級變量就加一,每遇到一個}
,層級變量就減一,分數(shù)變量加上一個層級變量。