annotate 2017/day10.d @ 10:bd993cf00e2f

day 10
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Mon, 11 Dec 2017 19:50:57 -0500
parents
children 990a4fa2aebe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
1 import std.stdio;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
2 import std.range: iota, array, chunks;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 import std.algorithm: invert=reverse, map, reduce;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
4 import std.conv: to;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
5 import std.format: format;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6 import std.string: join;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 void reverse(int Size)(ref int[Size] twine, int a, int b) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9 a = a % Size;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 b = b % Size;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 if (a <= b) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 invert(twine[a..b]);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 else {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15 auto result = twine[a..$] ~ twine[0..b];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 invert(result);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 twine[a..$] = result[0..Size-a];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 twine[0..b] = result[Size-a..$];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22 auto knotUp(int Size)(int[] lengths, int[Size] twine) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 static pos = 0, skip = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24 foreach(length; lengths) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 reverse(twine, pos, pos+length);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 pos += length + skip;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 skip++;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
29 return twine;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
30 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
31
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
32 auto getHash(int Size, int Rounds, int ChunkSize)(int[] lengths)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
33 if( Size % ChunkSize == 0)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
34 {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
35 int[Size] twine = iota(0, Size).array;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
36 for(int i = 0; i < Rounds; i++) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
37 twine = knotUp(lengths, twine);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
38 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
39 return twine.array.chunks(ChunkSize).map!(
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
40 x => format("%02x", reduce!((a,b) => a ^ b)(x[0], x[1..$]))
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
41 ).join;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
42 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
43
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
44 void main(string[] args){
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
45 int[] lengths = to!(int[])(cast(ubyte[]) args[1]) ~ [17, 31, 73, 47, 23];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
46 auto hash = getHash!(256, 64, 16)(lengths);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
47 writeln(hash);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
48 }