解題語(yǔ)言不限Java
謎題還有第二部分,不過是留給大家的,能解出第一題的,才能寫第二題
學(xué)生黨,今天課比較多,沒在晚上搞完。
- 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 注冊(cè)表愛好者
- Advent of Code Day 9 流處理
- Advent of Code Day 11 六邊形迷宮
題目?jī)?nèi)容
An urgent interrupt arrives from the CPU: it's trapped in a maze of jump instructions, and it would like assistance from any programs with spare cycles to help find the exit.
一個(gè)從CPU來的緊急中斷到達(dá),CPU被困在了一個(gè)跳躍的迷宮里。它需要來著空閑幫助才可以從這個(gè)迷宮里出來。
The message includes a list of the offsets for each jump. Jumps are relative: -1 moves to the previous instruction, and 2 skips the next one. Start at the first instruction in the list. The goal is to follow the jumps until one leads outside the list.
這條消息包含了一個(gè)跳躍偏移量列表。跳躍是相對(duì)的:-1 會(huì)把指針移到前一個(gè)項(xiàng),2 會(huì)跳過一個(gè)。從列表的第一個(gè)開始,到跳出最后一個(gè)為止。
In addition, these instructions are a little strange; after each jump, the offset of that instruction increases by 1. So, if you come across an offset of 3, you would move three instructions forward, but change it to a 4 for the next time it is encountered.
此外,每當(dāng)跳躍一次后,原來的偏移量會(huì)加一。所以,如果你從偏移量3起跳,你會(huì)向前跳三個(gè)然后原來的那個(gè)偏移量變成4。
For example, consider the following list of jump offsets:
比如說,看下這段跳躍偏移量列表:
0
3
0
1
-3
Positive jumps ("forward") move downward; negative jumps move upward. For legibility in this example, these offset values will be written all on one line, with the current instruction marked in parentheses. The following steps would be taken before an exit is found:
正值的偏移量會(huì)向前移動(dòng),負(fù)值的偏移量會(huì)向后移動(dòng)。 為了更加方便閱讀,這些跳躍偏移量會(huì)被寫到一行里。當(dāng)前指針讀取的偏移量會(huì)用括號(hào)標(biāo)注。
before we have taken any steps.
在進(jìn)行任何移動(dòng)前
(0) 3 0 1 -3jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
以偏移量0來做跳躍(并沒有移動(dòng))。幸運(yùn)的是, 這個(gè)偏移量會(huì)在之后加一。
(1) 3 0 1 -3step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
因?yàn)槠屏孔兓耍灾羔槕?yīng)該向前跳1,。 然后原先的偏移量加一,現(xiàn)在是2。
2 (3) 0 1 -3jump all the way to the end; leave a 4 behind.
跳到列表末尾,原偏移量變成4。
2 4 0 1 (-3)go back to where we just were; increment -3 to -2.
返回原來的位置,末尾的偏移量從-3變成-2。
2 (4) 0 1 -2jump 4 steps forward, escaping the maze.
跳向前跳四步,跳出列表
2 5 0 1 -2
In this example, the exit is reached in 5 steps.
在這個(gè)例子里,從
How many steps does it take to reach the exit?
要走多少步才跳出列表?
解題思路
這一題比較簡(jiǎn)單,只要讀取完所有的數(shù)值之后,將數(shù)值放入數(shù)組中。然后按照題目里說的一個(gè)個(gè)值操作就好了。