Advent of Code Day 8 注册表爱好者

解题语言不限Java

题目内容

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 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. 运行完成之后,检查注册表值,并查找最大值。
    原文作者:宅纸箱
    原文地址: https://www.jianshu.com/p/3a3e0a6d57ee
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞