Last active
August 29, 2015 14:02
-
-
Save cciollaro/6f71fc651e55f0e3c024 to your computer and use it in GitHub Desktop.
ghost parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
alias :peek :last | |
end | |
trials = gets.to_i | |
trials.times do |x| | |
stack = [] | |
ctr = 0 | |
ptr = 0 | |
str = gets.chomp | |
while ptr < str.length | |
if str[ptr, 5] == "GHOST" | |
ctr += 1 | |
ptr += 5 | |
elsif str[ptr,4] == "GHOS" | |
stack.push("GHOS") | |
ptr += 4 | |
elsif str[ptr,3] == "GHO" | |
stack.push("GHO") | |
ptr += 3 | |
elsif str[ptr,2] == "GH" | |
stack.push("GH") | |
ptr += 2 | |
elsif str[ptr,1] == "G" | |
stack.push("G") | |
ptr += 1 | |
elsif str[ptr,4] == "HOST" && stack.peek == "G" | |
stack.pop | |
ptr += 4 | |
ctr += 1 | |
elsif str[ptr,3] == "OST" && stack.peek == "GH" | |
stack.pop | |
ptr += 3 | |
ctr += 1 | |
elsif str[ptr,2] == "ST" && stack.peek == "GHO" | |
stack.pop | |
ptr += 2 | |
ctr += 1 | |
elsif str[ptr,1] == "T" && stack.peek == "GHOS" | |
stack.pop | |
ptr += 1 | |
ctr += 1 | |
else | |
stack = [] | |
ptr += 1 | |
end | |
end | |
puts "Case #{x+1}: #{ctr}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment