Mercurial > hg > aoc
comparison 2017/day09/app.d @ 33:bc652fa0a645
Move all solutions to per-day subdirs
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 09 Jan 2018 21:50:37 -0500 |
parents | 2017/day09.d@18e7ffa83a14 |
children |
comparison
equal
deleted
inserted
replaced
32:763c88851b91 | 33:bc652fa0a645 |
---|---|
1 import std.stdio; | |
2 import std.range; | |
3 | |
4 auto parseStream(string stream) { | |
5 bool quoted = false; | |
6 bool escape = false; | |
7 int sum = 0; | |
8 int garbage = 0; | |
9 int depth = 0; | |
10 foreach(c; stream) { | |
11 if(escape) { | |
12 escape = false; | |
13 continue; | |
14 } | |
15 else if(quoted) { | |
16 switch(c) { | |
17 case '>': | |
18 quoted = false; | |
19 break; | |
20 case '!': | |
21 escape = true; | |
22 break; | |
23 default: | |
24 garbage++; | |
25 } | |
26 } | |
27 else { | |
28 switch(c) { | |
29 case '{': | |
30 depth++; | |
31 break; | |
32 case '}': | |
33 sum += depth; | |
34 depth--; | |
35 break; | |
36 case '<': | |
37 quoted = true; | |
38 break; | |
39 default: | |
40 } | |
41 } | |
42 } | |
43 | |
44 return [sum, garbage]; | |
45 } | |
46 | |
47 void main(string[] args){ | |
48 writeln(File(args[1]).readln.parseStream); | |
49 } |