annotate 2017/day09/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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
1 import std.stdio;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
2 import std.range;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
4 auto parseStream(string stream) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
5 bool quoted = false;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6 bool escape = false;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7 int sum = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 int garbage = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9 int depth = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 foreach(c; stream) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 if(escape) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 escape = false;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13 continue;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15 else if(quoted) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 switch(c) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 case '>':
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 quoted = false;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 break;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 case '!':
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21 escape = true;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22 break;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 default:
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24 garbage++;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 else {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 switch(c) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
29 case '{':
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
30 depth++;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
31 break;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
32 case '}':
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
33 sum += depth;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
34 depth--;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
35 break;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
36 case '<':
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
37 quoted = true;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
38 break;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
39 default:
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
40 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
41 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
42 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
43
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
44 return [sum, garbage];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
45 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
46
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
47 void main(string[] args){
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
48 writeln(File(args[1]).readln.parseStream);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
49 }