annotate 2017/day05.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 20d440f0793e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
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.array: array;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 import std.stdio;
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 walkmaze(numType)(numType[] instructions) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7 auto steps = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 auto idx = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 while(idx >= 0 && idx < instructions.length) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 auto motion = instructions[idx];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 if (motion > 2) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13 instructions[idx]--;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15 else {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 instructions[idx]++;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 idx += motion;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 steps++;
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 return steps;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 void main(string[] args) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 auto instructions =
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 File(args[1])
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 .byLine
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
29 .map!(x => to!int(x))
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
30 .array;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
31
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
32 writeln(walkmaze(instructions));
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
33 }