view 2017/day05/app.d @ 33:bc652fa0a645

Move all solutions to per-day subdirs
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 09 Jan 2018 21:50:37 -0500
parents 2017/day05.d@20d440f0793e
children
line wrap: on
line source

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));
}