changeset 9:18e7ffa83a14

day 9
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sat, 09 Dec 2017 21:27:52 -0500
parents 7d06d033b0ee
children bd993cf00e2f
files 2017/day09.d
diffstat 1 files changed, 49 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/2017/day09.d
@@ -0,0 +1,49 @@
+import std.stdio;
+import std.range;
+
+auto parseStream(string stream) {
+  bool quoted = false;
+  bool escape = false;
+  int sum = 0;
+  int garbage = 0;
+  int depth = 0;
+  foreach(c; stream) {
+    if(escape) {
+      escape = false;
+      continue;
+    }
+    else if(quoted) {
+      switch(c) {
+      case '>':
+        quoted = false;
+        break;
+      case '!':
+        escape = true;
+        break;
+      default:
+        garbage++;
+      }
+    }
+    else {
+      switch(c) {
+      case '{':
+        depth++;
+        break;
+      case '}':
+        sum += depth;
+        depth--;
+        break;
+      case '<':
+        quoted = true;
+        break;
+      default:
+      }
+    }
+  }
+
+  return [sum, garbage];
+}
+
+void main(string[] args){
+  writeln(File(args[1]).readln.parseStream);
+}