comparison 2017/day06.d @ 6:94a2bb2aad6a

day 6
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Thu, 07 Dec 2017 11:44:54 -0500
parents
children
comparison
equal deleted inserted replaced
5:20d440f0793e 6:94a2bb2aad6a
1 import std.algorithm: map;
2 import std.conv: to;
3 import std.array: array;
4 import std.range: enumerate;
5 import std.stdio;
6 import std.algorithm: maxElement;
7
8 auto redistribute(numType)(numType[] membank) {
9 auto max = membank.enumerate.maxElement!"a.value";
10 membank[max.index] = 0;
11 for(auto idx = 0; idx < max.value; idx++) {
12 auto circ_idx = (max.index + idx + 1) % membank.length;
13 membank[circ_idx]++;
14 }
15 return membank;
16 }
17
18 auto waitForDupe(numType)(numType[] membank) {
19 numType steps = 1;
20 auto seen = [membank.idup: steps];
21 writeln(membank);
22 while( membank.redistribute !in seen) {
23 seen[membank.idup] = steps;
24 steps++;
25 }
26
27 return [steps, steps - seen[membank]];
28 }
29
30 void main(string[] args) {
31 auto membank = args[1..$].map!(to!int).array;
32 writeln(waitForDupe(membank));
33 }