Advent of Code Day 9 流處理

解題語言不限Java

個人感覺這個難哭了,主要還是沒干過這個

拖更了,不好意思

題目內(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?
    你的謎題輸入的總分是多少?

解題思路

萌新剛剛看到題的時候被嚇了一跳,還好我們的老油條,企鵝給了個方便的做法。
在此感謝下企鵝。

這個題目的思路主要是:

  1. 要能合理解析垃圾和跳躍字符
  2. 要對{}的層級進行正確解析

企鵝大佬的做法是

  1. 建一個for循環(huán)遍歷其中所有的字符。
  2. 在循環(huán)中,如果檢測到!就跳過一個字符
  3. 如果檢測到<,就開始忽略掉字符,直到檢測到>
  4. 定義一個層級變量和一個分數(shù)變量。每遇到一個{,層級變量就加一,每遇到一個},層級變量就減一,分數(shù)變量加上一個層級變量。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,934評論 0 23
  • 前言 上篇文章介紹了常用的IDE,這一篇介紹搭載完善插件的VSCode進行Vue開發(fā)。Vue的組件有獨立格式.vu...
    Mr_Treasure閱讀 6,579評論 8 19
  • 手里握著從爸爸那拿來的三菱針管筆在稿紙上抄寫單詞,筆水明顯地滲透在紙上,耳機里播放著zion.t的新專輯,原裝的聲...
    wuli率毛閱讀 180評論 0 1
  • 哎呦,這是哪兒?風景不錯呦! 瞧,草綠綠,水清清,還有白鷺呢~ 濕地?保護區(qū)? NO,它只是家門口的一條小河 景色...
    紫竹院士閱讀 290評論 0 0
  • 元認知能力:對自己的思考過程的認知與理解。以前我們做事可能只是簡單地做,以為自己知道了,沒有深入地去了解和探索,并...
    淇淇18閱讀 334評論 3 3