Skip to content

Commit 0256b63

Browse files
committed
Day 09
1 parent 390cbfe commit 0256b63

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

09_stream_of_garbage.rb

+33
Original file line numberDiff line numberDiff line change
@@ -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+
end
30+
}
31+
32+
puts score
33+
puts garbages

0 commit comments

Comments
 (0)