Advent of Code Day 9 流处理

解题语言不限Java

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

拖更了,不好意思

  • Advent of Code Day 1 逆向验证码
  • Advent of Code Day 2 损坏校验和
  • Advent of Code Day 3 螺旋内存
  • Advent of Code Day 4 高熵密码
  • Advevnt of Code Day 5 曲折的蹦床迷宫
  • Advent of Code Day 6 内存重分配
  • Advent of Code Day 7 递归马戏团
  • Advent of Code Day 8 注册表爱好者
  • Advent of Code Day 9 流处理
  • Advent of Code Day 10 结哈希
  • Advent of Code Day 11 六边形迷宫

题目内容

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:
这里是一些单独的垃圾:

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个大组包含,那分数为三。

解题思路

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

这个题目的思路主要是:

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

企鹅大佬的做法是

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

你可能感兴趣的:(Advent of Code Day 9 流处理)