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. 一个是检索函数,把当前状态和历史状态进行比较来找到相同的状态
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容