解題語(yǔ)言不限Java
謎題還有第二部分曹质,不過(guò)是留給大家的糟描,能解出第一題的著瓶,才能寫(xiě)第二題
又鴿了一天才翻完洒扎。
- 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è)表愛(ài)好者
- Advent of Code Day 9 流處理
- Advent of Code Day 11 六邊形迷宮
題目?jī)?nèi)容
A debugger program here is having an issue: it is trying to repair a memory reallocation routine, but it keeps getting stuck in an infinite loop.
一個(gè)調(diào)試程序出了問(wèn)題:它嘗試重置一個(gè)內(nèi)存重分配事務(wù),但是它陷入了一個(gè)死循環(huán)檐薯。
In this area, there are sixteen memory banks; each memory bank can hold any number of blocks. The goal of the reallocation routine is to balance the blocks between the memory banks.
在這個(gè)區(qū)域有16個(gè)內(nèi)存堆,每一個(gè)內(nèi)存堆可以保持任意數(shù)量的內(nèi)存塊。這個(gè)事務(wù)的目標(biāo)是平均內(nèi)存塊到所有的內(nèi)存堆坛缕。
The reallocation routine operates in cycles. In each cycle, it finds the memory bank with the most blocks (ties won by the lowest-numbered memory bank) and redistributes those blocks among the banks. To do this, it removes all of the blocks from the selected bank, then moves to the next (by index) memory bank and inserts one of the blocks. It continues doing this until it runs out of blocks; if it reaches the last memory bank, it wraps around to the first one.
這個(gè)事務(wù)運(yùn)行在一個(gè)循環(huán)里墓猎。每次循環(huán),它會(huì)找到存有最多內(nèi)存塊的內(nèi)存堆(如果有多個(gè)最大值赚楚,則取第一個(gè)堆)然后重新分配其中的內(nèi)存塊到其他的堆毙沾。為了達(dá)到目標(biāo),這個(gè)事務(wù)會(huì)提取目標(biāo)堆得內(nèi)存塊宠页,然后移動(dòng)到下一個(gè)堆并將一個(gè)內(nèi)存塊放入直到全部分配完畢左胞。如果放入的位置超過(guò)堆列表的長(zhǎng)度,它會(huì)移動(dòng)到列表開(kāi)頭举户。
The debugger would like to know how many redistributions can be done before a blocks-in-banks configuration is produced that has been seen before.
這個(gè)調(diào)試程序想要知道在多少次重新分配之后會(huì)出現(xiàn)死循環(huán)烤宙。
For example, imagine a scenario with only four memory banks:
舉個(gè)例子,想像一個(gè)只有4個(gè)內(nèi)存堆的列表 0,2,7,0
The third bank has the most blocks, so it is chosen for redistribution.
第三個(gè)堆有最多的塊俭嘁,所以程序會(huì)對(duì)這個(gè)堆進(jìn)行重新分配躺枕。
Starting with the next bank (the fourth bank) and then continuing to the first bank, the second bank, and so on, the 7 blocks are spread out over the memory banks. The fourth, first, and second banks get two blocks each, and the third bank gets one back. The final result looks like this: 2 4 1 2.
從下一個(gè)堆(第四個(gè))開(kāi)始,然后到第一個(gè)堆供填,第二個(gè)堆……拐云,所有七個(gè)塊都會(huì)被分配到每一個(gè)內(nèi)存堆里。第一近她,二叉瘩,四堆會(huì)被分到2,第三個(gè)堆會(huì)被分到1粘捎。最后的結(jié)果是2,4,1,2
薇缅。
Next, the second bank is chosen because it contains the most blocks (four).
接下來(lái),第二個(gè)堆是最多的(4個(gè))晌端。
Because there are four memory banks, each gets one block. The result is: 3 1 2 3.
因?yàn)檫@個(gè)有四個(gè)塊捅暴,所以每個(gè)堆都加一。3,1,2,3
Now, there is a tie between the first and fourth memory banks, both of which have three blocks. The first bank wins the tie, and its three blocks are distributed evenly over the other three banks, leaving it with none: 0 2 3 4.
現(xiàn)在有兩個(gè)相等的值咧纠,第一個(gè)和第四個(gè)蓬痒。這兩個(gè)都有三個(gè)塊。第一個(gè)堆因?yàn)閿?shù)字比較小漆羔,所以被選中梧奢,其中的塊被平均分配到每一個(gè)堆。最后結(jié)果是0,2,3,4
演痒。
The fourth bank is chosen, and its four blocks are distributed such that each of the four banks receives one: 1 3 4 1.
現(xiàn)在亲轨,第四個(gè)堆被選中,然后其中的四個(gè)塊被分配到每一個(gè)堆里:1,3,4,1
The third bank is chosen, and the same thing happens: 2 4 1 2.
現(xiàn)在鸟顺,第三個(gè)堆被選中惦蚊,同樣的事情發(fā)生了:2,4,1,2
At this point, we've reached a state we've seen before: 2 4 1 2 was already seen. The infinite loop is detected after the fifth block redistribution cycle, and so the answer in this example is 5.
這時(shí)器虾,我們到一個(gè)重復(fù)發(fā)生的狀態(tài):2,4,1,2
所以我們?cè)诘谖宀秸业搅艘粋€(gè)死循環(huán),所以答案是5
Given the initial block counts in your puzzle input, how many redistribution cycles must be completed before a configuration is produced that has been seen before?
請(qǐng)問(wèn)蹦锋,根據(jù)你的謎題輸入兆沙,多少次重分配之后會(huì)得到一個(gè)死循環(huán)。
解題思路
這個(gè)題目基本上莉掂,沒(méi)有很多要分析的地方葛圃,所以我只放解法。
我做了三個(gè)函數(shù)
- 一個(gè)是分配函數(shù)憎妙,將選定的點(diǎn)分配到所有得到堆
- 一個(gè)是搜索函數(shù)库正,找到最大值
- 一個(gè)是檢索函數(shù),把當(dāng)前狀態(tài)和歷史狀態(tài)進(jìn)行比較來(lái)找到相同的狀態(tài)