annotate 2017/day03.d @ 12:a991288f3d55 tip @

day 11: simplify hexNorm
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Mon, 11 Dec 2017 22:24:25 -0500
parents b5533de6ff5b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
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.math: realabs=abs;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 import std.complex;
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
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6 auto generateSpiral(numType)(numType x) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7 auto z = Complex!numType(0);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 auto i = Complex!numType(0, 1);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9 auto direction = Complex!numType(1);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 numType stepsTillTurn = 1, stepsTaken = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 bool maxedOnce = false;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13 auto result = [z];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15 for(numType steps = 1; steps < x; steps++) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 z += direction;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 stepsTaken++;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 result ~= z;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 if(stepsTaken == stepsTillTurn) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 // Time to turn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21 direction *= i;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22 stepsTaken = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 if(maxedOnce) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24 stepsTillTurn++;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 maxedOnce = false;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 else {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 maxedOnce = true;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
29 }
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 return result;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
33 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
34
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
35 auto tallySpiral(numType)(Complex!numType[] spiral) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
36 auto tally = [numType(1)];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
37
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
38 for(auto i = 1; i < spiral.length; i++) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
39 numType sum = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
40 auto z = spiral[i];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
41 for(auto j = 0; j < i; j++) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
42 auto w = spiral[j];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
43 if(abs(w - z) < 2) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
44 sum += tally[j];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
45 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
46 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
47 tally ~= sum;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
48 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
49
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
50 return tally;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
51 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
52
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
53 void main(string[] args){
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
54 auto target = to!double(args[1]);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
55 auto spiral = generateSpiral(target);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
56 auto end = spiral[$-1];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
57 writeln(realabs(end.re) + realabs(end.im));
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
58 auto tally = tallySpiral(spiral);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
59 writeln(tally);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
60 }