Mercurial > hg > aoc
comparison 2017/day06/app.d @ 33:bc652fa0a645
Move all solutions to per-day subdirs
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 09 Jan 2018 21:50:37 -0500 |
parents | 2017/day06.d@94a2bb2aad6a |
children |
comparison
equal
deleted
inserted
replaced
32:763c88851b91 | 33:bc652fa0a645 |
---|---|
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 } |