解题语言不限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 六边形迷宫
题目内容
You receive a signal directly from the CPU. Because of your recent assistance with jump instructions, it would like you to compute the result of a series of unusual register instructions.
你收到一条从CPU 来的消息,因为你帮助解决了跳转指令,CPU想让你帮忙计算一系列奇怪的注册表指令。
Each instruction consists of several parts: the register to modify, whether to increase or decrease that register's value, the amount by which to increase or decrease it, and a condition. If the condition fails, skip the instruction without modifying the register. The registers all start at 0. The instructions look like this:
每个命令分为几个部分,要变更的注册表项,增加或者减少指令,增加或者减少的量,还有一个条件。如果不符合条件,就跳过这个结果。所有的注册表项初始值都为零。
b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10
These instructions would be processed as follows:
这些指令会经过一下过程。
Because
astarts at0, it is not greater than1, and sobis not modified.
因为a的起始值是0,不大于1,所以b不会变化。ais increased by1(to1) becausebis less than5(it is0).
a增加1因为b小于5。cis decreased by-10(to10) becauseais now greater than or equal to1(it is1).
c减少了-10因为a与等于1。cis increased by-20(to-10) becausecis equal to10.
c增加-20因为c与等于10。
After this process, the largest value in any register is1.
在这些步骤之后,在注册表里最大的项是1。
You might also encounter<=(less than or equal to) or!=(not equal to). However, the CPU doesn't have the bandwidth to tell you what all the registers are named, and leaves that to you to determine.
你应该考虑小于等于(<=)和不等于(!=)。但是,CPU并没有带宽告诉你所有的注册表项,这需要你自己去搞定。
What is the largest value in any register after completing the instructions in your puzzle input?
在给出的操作完成之后,最大的注册表项等于什么?
解题思路
这个题,我个人强力推荐HashMap作为储存。
基本步骤如下:
- 读取并解析所以的指令,具体可以按照指令的格式
目标项+增减+增减量+if+条件项+条件+条件量。 - 在读取命令的时候,把所有的注册表项都加入到HashMap里。
- 按顺序运行所有命令。
- 运行完成之后,检查注册表值,并查找最大值。