解題語言不限Java
- Advent of Code Day 1 逆向驗(yàn)證碼
- Advent of Code Day 2 損壞校驗(yàn)和
- 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 遞歸馬戲團(tuán)
- 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 來的消息娄琉,因?yàn)槟銕椭鉀Q了跳轉(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:
每個命令分為幾個部分恼除,要變更的注冊表項(xiàng)橡淑,增加或者減少指令彩届,增加或者減少的量,還有一個條件繁疤。如果不符合條件怕敬,就跳過這個結(jié)果揣炕。所有的注冊表項(xiàng)初始值都為零。
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.
因?yàn)?code>a的起始值是0
畸陡,不大于1
,所以b
不會變化虽填。a
is increased by1
(to1
) becauseb
is less than5
(it is0
).
a
增加1
因?yàn)?code>b小于5
丁恭。c
is decreased by-10
(to10
) becausea
is now greater than or equal to1
(it is1
).
c
減少了-10
因?yàn)?code>a與等于1
。c
is increased by-20
(to-10
) becausec
is equal to10
.
c
增加-20
因?yàn)?code>c與等于10
卤唉。
After this process, the largest value in any register is1
.
在這些步驟之后涩惑,在注冊表里最大的項(xiàng)是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并沒有帶寬告訴你所有的注冊表項(xiàng)跛蛋,這需要你自己去搞定熬的。
What is the largest value in any register after completing the instructions in your puzzle input?
在給出的操作完成之后,最大的注冊表項(xiàng)等于什么赊级?
解題思路
這個題押框,我個人強(qiáng)力推薦HashMap作為儲存。
基本步驟如下:
- 讀取并解析所以的指令理逊,具體可以按照指令的格式
目標(biāo)項(xiàng)+增減+增減量+if+條件項(xiàng)+條件+條件量
橡伞。 - 在讀取命令的時候,把所有的注冊表項(xiàng)都加入到HashMap里晋被。
- 按順序運(yùn)行所有命令兑徘。
- 運(yùn)行完成之后,檢查注冊表值羡洛,并查找最大值挂脑。