-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.pas
67 lines (59 loc) · 1.54 KB
/
stream.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Program day09; {$MODE OBJFPC} {$COPERATORS ON}
Var
Stream: AnsiString;
Procedure ProcessGarbage(Idx: NativeInt; Out EndIdx, CharCount:NativeInt);
Begin
Idx += 1; // Skip the opening '<'
CharCount := 0;
While (Stream[Idx] <> '>') do begin
If(Stream[Idx] = '!') then begin
Idx += 2
end else begin
CharCount += 1;
Idx += 1
end
end;
EndIdx := Idx
End;
Procedure ProcessGroup(Const StartIdx, Depth: NativeInt; Out EndIdx, TotalDepth, GarbageChars:NativeInt);
Var
Idx, DepthSum, HowMuchGarbage: NativeInt;
subgroup_end, subgroup_depth: NativeInt;
garbage_end, garbage_count: NativeInt;
Begin
DepthSum := Depth;
HowMuchGarbage := 0;
Idx := StartIdx+1;
While (Stream[Idx] <> '}') do begin
If(Stream[Idx] = '{') then begin
ProcessGroup(Idx, Depth+1, subgroup_end, subgroup_depth, garbage_count);
HowMuchGarbage += garbage_count;
DepthSum += subgroup_depth;
Idx := subgroup_end + 1
end else If(Stream[Idx] = '<') then begin
ProcessGarbage(Idx, garbage_end, garbage_count);
HowMuchGarbage += garbage_count;
Idx := garbage_end + 1
end else If(Stream[Idx] = ',') then begin
Idx += 1
end else begin
Writeln(stderr, 'watafuq? unexpected character "', Stream[Idx], '"')
end
end;
EndIdx := Idx;
TotalDepth := DepthSum;
GarbageChars := HowMuchGarbage
End;
Procedure ProcessInput();
Var
Ignore, Depth, Garbage: NativeInt;
Begin
ProcessGroup(1, 1, Ignore, Depth, Garbage);
Writeln('-- depth: ', Depth, '; garbage: ', garbage)
End;
Begin
While Not eof() do begin
Readln(Stream);
ProcessInput()
end
End.