changeset 22:4e7e7835f1db

day 17
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Mon, 18 Dec 2017 16:42:47 -0500
parents f1f0220b5f7c
children 5cf02601cd14
files 2017/day17.d
diffstat 1 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/2017/day17.d
@@ -0,0 +1,31 @@
+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));
+}