annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
1 import std.algorithm: map;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
2 import std.conv: to;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 import std.array: array;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
4 import std.range: enumerate;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
5 import std.stdio;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6 import std.algorithm: maxElement;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 auto redistribute(numType)(numType[] membank) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9 auto max = membank.enumerate.maxElement!"a.value";
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 membank[max.index] = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 for(auto idx = 0; idx < max.value; idx++) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 auto circ_idx = (max.index + idx + 1) % membank.length;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13 membank[circ_idx]++;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15 return membank;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 auto waitForDupe(numType)(numType[] membank) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 numType steps = 1;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 auto seen = [membank.idup: steps];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21 writeln(membank);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22 while( membank.redistribute !in seen) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 seen[membank.idup] = steps;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24 steps++;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 return [steps, steps - seen[membank]];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
29
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
30 void main(string[] args) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
31 auto membank = args[1..$].map!(to!int).array;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
32 writeln(waitForDupe(membank));
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
33 }