解題語言不限Java
- Advent of Code Day 1 逆向驗證碼
- Advent of Code Day 2 損壞校驗和
- Advent of Code Day 3 螺旋內(nèi)存
- Advent of Code Day 4 高熵密碼
- Advevnt of Code Day 5 曲折的蹦床迷宮
- Advent of Code Day 6 內(nèi)存重分配
- Advent of Code Day 7 遞歸馬戲團
- Advent of Code Day 8 注冊表愛好者
- Advent of Code Day 9 流處理
- Advent of Code Day 10 結(jié)哈希
- Advent of Code Day 11 六邊形迷宮
題目內(nèi)容
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 來的消息,因為你幫助解決了跳轉(zhuǎn)指令,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:
每個命令分為幾個部分,要變更的注冊表項,增加或者減少指令,增加或者減少的量,還有一個條件。如果不符合條件,就跳過這個結(jié)果。所有的注冊表項初始值都為零。
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:
這些指令會經(jīng)過一下過程。
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.
你應(yīng)該考慮小于等于(<=
)和不等于(!=
)。但是,CPU并沒有帶寬告訴你所有的注冊表項,這需要你自己去搞定。
What is the largest value in any register after completing the instructions in your puzzle input?
在給出的操作完成之后,最大的注冊表項等于什么?
解題思路
這個題,我個人強力推薦HashMap作為儲存。
基本步驟如下:
- 讀取并解析所以的指令,具體可以按照指令的格式
目標(biāo)項+增減+增減量+if+條件項+條件+條件量
。 - 在讀取命令的時候,把所有的注冊表項都加入到HashMap里。
- 按順序運行所有命令。
- 運行完成之后,檢查注冊表值,并查找最大值。