view 2017/day17/app.d @ 35:1d99d733cf13 default tip @

day08: replace static foreach with workaround
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 16 Jan 2018 11:28:55 -0500
parents bc652fa0a645
children
line wrap: on
line source

import std.stdio;
import std.algorithm: find;
import std.conv: to;

auto getBuffer(ulong skip) {
  ulong[] circ = [0];
  ulong pos = 0;
  foreach(y; 1..2018) {
    pos = (pos + skip) % circ.length + 1;
    circ = circ[0..pos] ~ y ~ circ[pos..$];
  }
  return circ;
}

auto getPastZero(ulong skip) {
  ulong pos = 0;
  ulong pastzero = 0;
  foreach(length; 1..50_000_000) {
    pos = (pos + skip) % length + 1;
    if (pos == 1) {
      pastzero = length;
    }
  }
  return pastzero;
}

void main(string[] args) {
  auto circ = getBuffer(args[1].to!ulong);
  writeln(find(circ, 2017)[1]);
  writeln(getPastZero(args[1].to!ulong));
}