Mercurial > hg > aoc
comparison 2017/day05.d @ 5:20d440f0793e
day 5
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Thu, 07 Dec 2017 11:44:53 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:c7b6dfd6eba6 | 5:20d440f0793e |
---|---|
1 import std.algorithm: map; | |
2 import std.array: array; | |
3 import std.stdio; | |
4 import std.conv: to; | |
5 | |
6 auto walkmaze(numType)(numType[] instructions) { | |
7 auto steps = 0; | |
8 auto idx = 0; | |
9 | |
10 while(idx >= 0 && idx < instructions.length) { | |
11 auto motion = instructions[idx]; | |
12 if (motion > 2) { | |
13 instructions[idx]--; | |
14 } | |
15 else { | |
16 instructions[idx]++; | |
17 } | |
18 idx += motion; | |
19 steps++; | |
20 } | |
21 | |
22 return steps; | |
23 } | |
24 | |
25 void main(string[] args) { | |
26 auto instructions = | |
27 File(args[1]) | |
28 .byLine | |
29 .map!(x => to!int(x)) | |
30 .array; | |
31 | |
32 writeln(walkmaze(instructions)); | |
33 } |