view 2017/day09.d @ 9:18e7ffa83a14

day 9
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sat, 09 Dec 2017 21:27:52 -0500
parents
children
line wrap: on
line source

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