Advent of Code Day 8 注冊表愛好者

解題語言不限Java

題目內(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 at 0, it is not greater than 1, and so b is not modified.
    因為a的起始值是0,不大于1,所以b不會變化。

  • a is increased by 1(to1) becauseb is less than 5(it is 0).
    a 增加1因為b小于5。

  • cis decreased by -10 (to 10) because a is now greater than or equal to 1 (it is 1).
    c減少了-10因為a與等于1。

  • cis increased by-20(to -10) because c 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作為儲存。
基本步驟如下:

  1. 讀取并解析所以的指令,具體可以按照指令的格式目標項+增減+增減量+if+條件項+條件+條件量
  2. 在讀取命令的時候,把所有的注冊表項都加入到HashMap里。
  3. 按順序運行所有命令。
  4. 運行完成之后,檢查注冊表值,并查找最大值。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。