Advent of Code Day 6 內存重分配

解題語言不限Java

謎題還有第二部分,不過是留給大家的,能解出第一題的,才能寫第二題

又鴿了一天才翻完。

題目內容

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.
一個調試程序出了問題:它嘗試重置一個內存重分配事務,但是它陷入了一個死循環。
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.
在這個區域有16個內存堆,每一個內存堆可以保持任意數量的內存塊。這個事務的目標是平均內存塊到所有的內存堆。
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.
這個事務運行在一個循環里。每次循環,它會找到存有最多內存塊的內存堆(如果有多個最大值,則取第一個堆)然后重新分配其中的內存塊到其他的堆。為了達到目標,這個事務會提取目標堆得內存塊,然后移動到下一個堆并將一個內存塊放入直到全部分配完畢。如果放入的位置超過堆列表的長度,它會移動到列表開頭。
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.
這個調試程序想要知道在多少次重新分配之后會出現死循環。
For example, imagine a scenario with only four memory banks:
舉個例子,想像一個只有4個內存堆的列表 0,2,7,0
The third bank has the most blocks, so it is chosen for redistribution.
第三個堆有最多的塊,所以程序會對這個堆進行重新分配。
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.
從下一個堆(第四個)開始,然后到第一個堆,第二個堆……,所有七個塊都會被分配到每一個內存堆里。第一,二,四堆會被分到2,第三個堆會被分到1。最后的結果是2,4,1,2
Next, the second bank is chosen because it contains the most blocks (four).
接下來,第二個堆是最多的(4個)。
Because there are four memory banks, each gets one block. The result is: 3 1 2 3.
因為這個有四個塊,所以每個堆都加一。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.
現在有兩個相等的值,第一個和第四個。這兩個都有三個塊。第一個堆因為數字比較小,所以被選中,其中的塊被平均分配到每一個堆。最后結果是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.
現在,第四個堆被選中,然后其中的四個塊被分配到每一個堆里:1,3,4,1
The third bank is chosen, and the same thing happens: 2 4 1 2.
現在,第三個堆被選中,同樣的事情發生了: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.
這時,我們到一個重復發生的狀態:2,4,1,2 所以我們在第五步找到了一個死循環,所以答案是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?
請問,根據你的謎題輸入,多少次重分配之后會得到一個死循環。

解題思路

這個題目基本上,沒有很多要分析的地方,所以我只放解法。

我做了三個函數

  1. 一個是分配函數,將選定的點分配到所有得到堆
  2. 一個是搜索函數,找到最大值
  3. 一個是檢索函數,把當前狀態和歷史狀態進行比較來找到相同的狀態
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容