We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 390cbfe commit 0256b63Copy full SHA for 0256b63
09_stream_of_garbage.rb
@@ -0,0 +1,33 @@
1
+input = ARGF.read.chomp
2
+
3
+# Remove the ! and characters they cancel.
4
+# This only works given that gsub does not find overlapping groups
5
+# (so that !!x turns into x, not empty string)
6
+# and that ! and the char it cancels aren't counted in garbage.
7
+input.gsub!(/!./, '')
8
9
+score = 0
10
+open_groups = 0
11
+garbage = false
12
+garbages = 0
13
14
+input.each_char { |x|
15
+ garbages += 1 if garbage && x != ?>
16
17
+ case x
18
+ when ?{
19
+ open_groups += 1 unless garbage
20
+ when ?}
21
+ unless garbage
22
+ score += open_groups
23
+ open_groups -= 1
24
+ end
25
+ when ?<
26
+ garbage = true
27
+ when ?>
28
+ garbage = false
29
30
+}
31
32
+puts score
33
+puts garbages
0 commit comments