Skip to content

Instantly share code, notes, and snippets.

@sarahzrf
Created December 10, 2021 18:17
Show Gist options
  • Save sarahzrf/3935c68dfe34cf42fd590ec40c0fb7b7 to your computer and use it in GitHub Desktop.
Save sarahzrf/3935c68dfe34cf42fd590ec40c0fb7b7 to your computer and use it in GitHub Desktop.
class Corrupted < Exception
def initialize(got)
@got = got
end
attr_reader :got
end
class Parser
def initialize(text)
@text = text
@pos = 0
@completion = ""
end
def eof?
@pos >= @text.length
end
def peek
@text[@pos] or raise EOFError
end
def pop
char = peek
@pos += 1
char
end
def unpop
@pos -= 1
end
def expect(char)
nchar = pop
raise Corrupted.new(nchar) if nchar != char
end
def parse_chunk
case nchar = pop
when '('
parse_chunk_body ')'
when '['
parse_chunk_body ']'
when '{'
parse_chunk_body '}'
when '<'
parse_chunk_body '>'
else
unpop
raise Corrupted.new(nchar)
end
end
def parse_chunk_body(delim)
loop do
if peek == delim
pop
break
end
parse_chunk
rescue EOFError
@completion << delim
raise
end
end
Scoring = {
')' => 1,
']' => 2,
'}' => 3,
'>' => 4,
}
def score
@completion.chars.reduce(0) {|n, c| n * 5 + (Scoring[c] or 0)}
end
end
scores = []
$<.each do |line|
p = Parser.new(line.chomp)
p.parse_chunk until p.eof?
rescue EOFError
scores << p.score
rescue Corrupted
end
print scores.sort[scores.length / 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment