# HG changeset patch # User Jordi GutiƩrrez Hermoso <jordigh@octave.org> # Date 1512665093 18000 # Node ID 20d440f0793e0aed05026a7df500fdbe51701067 # Parent c7b6dfd6eba69f095b0eda7496e8f14b18266169 day 5 diff --git a/2017/day05.d b/2017/day05.d new file mode 100644 --- /dev/null +++ b/2017/day05.d @@ -0,0 +1,33 @@ +import std.algorithm: map; +import std.array: array; +import std.stdio; +import std.conv: to; + +auto walkmaze(numType)(numType[] instructions) { + auto steps = 0; + auto idx = 0; + + while(idx >= 0 && idx < instructions.length) { + auto motion = instructions[idx]; + if (motion > 2) { + instructions[idx]--; + } + else { + instructions[idx]++; + } + idx += motion; + steps++; + } + + return steps; +} + +void main(string[] args) { + auto instructions = + File(args[1]) + .byLine + .map!(x => to!int(x)) + .array; + + writeln(walkmaze(instructions)); +}