-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-2.rb
executable file
·54 lines (44 loc) · 863 Bytes
/
10-2.rb
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
#!/usr/bin/env ruby
open = [ '(', '[', '{', '<' ]
close = [ ')', ']', '}', '>' ]
match = {
'(' => ')',
'[' => ']',
'{' => '}',
'<' => '>'
}
score_table = {
')' => 1,
']' => 2,
'}' => 3,
'>' => 4
}
lines = File.read('10.input').lines.map(&:strip).map { |l| l.split '' }
scores = []
lines.each do |line|
stack = []
corrupted = false
line.each do |token|
if open.any? token
stack.push token
elsif close.any? token
top = stack[-1]
if token == match[top]
stack.pop
else
corrupted = true
break
end
end
end
next if corrupted or stack.empty?
completion = stack.reverse.map { |token| match[token] }
score = 0
completion.each do |token|
score *= 5
score += score_table[token]
end
scores.append score
end
scores.sort!
print scores[scores.size / 2], "\n"