Advent of Code Day 9 流处理

作者: 宅纸箱 | 来源:发表于2017-12-11 06:04 被阅读5次

    解题语言不限Java

    个人感觉这个难哭了,主要还是没干过这个

    拖更了,不好意思

    题目内容

    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.)
    你的目标是找出输入里组的总分。每个组都被指定了一个分数,如果这个组被3个大组包含,那分数为三。

    • {}, score of 1.
      分数为1
    • {{{}}}, score of 1 + 2 + 3 = 6.
      分数为1
    • {{},{}}, score of 1 + 2 + 2 = 5.
      分数为1+2+2=5
    • {{{},{},{{}}}}, score of 1 + 2 + 3 + 3 + 3 + 4 = 16.
      分数为1 + 2 + 3 + 3 + 3 + 4 = 16
    • {<a>,<a>,<a>,<a>}, score of 1.
      分数为1
    • {{<ab>},{<ab>},{<ab>},{<ab>}}, score of 1 + 2 + 2 + 2 + 2 = 9.
      分数为1 + 2 + 2 + 2 + 2 = 9
    • {{<!!>},{<!!>},{<!!>},{<!!>}}, score of 1 + 2 + 2 + 2 + 2 = 9.
      分数为1 + 2 + 2 + 2 + 2 = 9.
    • {{<a!>},{<a!>},{<a!>},{<ab>}}, score of 1 + 2 = 3.
      分数为1 + 2 = 3
      What is the total score for all groups in your input?
      你的谜题输入的总分是多少?

    解题思路

    萌新刚刚看到题的时候被吓了一跳,还好我们的老油条,企鹅给了个方便的做法。
    在此感谢下企鹅。

    这个题目的思路主要是:

    1. 要能合理解析垃圾和跳跃字符
    2. 要对{}的层级进行正确解析

    企鹅大佬的做法是

    1. 建一个for循环遍历其中所有的字符。
    2. 在循环中,如果检测到!就跳过一个字符
    3. 如果检测到<,就开始忽略掉字符,直到检测到>
    4. 定义一个层级变量和一个分数变量。每遇到一个{,层级变量就加一,每遇到一个},层级变量就减一,分数变量加上一个层级变量。

    相关文章

      网友评论

        本文标题:Advent of Code Day 9 流处理

        本文链接:https://www.haomeiwen.com/subject/qgrmixtx.html