解题语言不限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
a
starts at0
, it is not greater than1
, and sob
is not modified.
因为a
的起始值是0
,不大于1
,所以b
不会变化。a
is increased by1
(to1
) becauseb
is less than5
(it is0
).
a
增加1
因为b
小于5
。c
is decreased by-10
(to10
) becausea
is now greater than or equal to1
(it is1
).
c
减少了-10
因为a
与等于1
。c
is increased by-20
(to-10
) becausec
is 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里。
- 按顺序运行所有命令。
- 运行完成之后,检查注册表值,并查找最大值。