changeset 5:20d440f0793e

day 5
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Thu, 07 Dec 2017 11:44:53 -0500
parents c7b6dfd6eba6
children 94a2bb2aad6a
files 2017/day05.d
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
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));
+}