[2021][ruby][10.x] refactoring?

pull/2/head
Alpha Chen 3 years ago
parent 96072c90c3
commit aa66d3ed86

@ -1,5 +1,3 @@
require "strscan"
input = ARGF.read.split("\n") input = ARGF.read.split("\n")
class IllegalCharacter < StandardError class IllegalCharacter < StandardError
@ -9,30 +7,19 @@ class IllegalCharacter < StandardError
end end
end end
COMPLEMENTS = %w[ () [] {} <> ].map(&:chars).to_h
OPENING_REGEXP = Regexp.new("[#{Regexp.escape(COMPLEMENTS.keys.join)}]")
CLOSING_REGEXP = Regexp.new("[#{Regexp.escape(COMPLEMENTS.values.join)}]")
def parse(line) def parse(line)
ss = StringScanner.new(line)
stack = [] stack = []
until ss.eos? s = line.chars
case until s.empty?
when ss.scan(/\(/) case char = s.shift
stack << ?( when OPENING_REGEXP
when ss.scan(/\[/) stack << char
stack << ?[ when CLOSING_REGEXP
when ss.scan(/\{/) raise IllegalCharacter.new(char) unless stack.pop == COMPLEMENTS.invert.fetch(char)
stack << ?{
when ss.scan(/</)
stack << ?<
when ss.scan(/\)/)
raise IllegalCharacter.new(?)) unless stack.pop == ?(
when ss.scan(/\]/)
raise IllegalCharacter.new(?]) unless stack.pop == ?[
when ss.scan(/\}/)
raise IllegalCharacter.new(?}) unless stack.pop == ?{
when ss.scan(/>/)
raise IllegalCharacter.new(?>) unless stack.pop == ?<
else
fail
end end
end end
@ -55,20 +42,12 @@ end
# end # end
# } # }
POINTS = ")]}>".chars.map.with_index.to_h.transform_values { _1 + 1 } def score(closing) = closing
def score(closing) .map { COMPLEMENTS.values.index(_1) + 1 }
total = 0 .inject(0) {|n,i| n*5 + i }
closing.each do |char|
total *= 5
total += POINTS.fetch(char)
end
total
end
complements = "() [] {} <>".split(" ").map(&:chars).to_h
scores = input scores = input
.map { parse(_1) rescue [] } .filter_map { parse(_1) rescue nil }
.reject(&:empty?) .map {|remaining| remaining.reverse.map { COMPLEMENTS.fetch(_1) }}
.map {|remaining| remaining.reverse.map { complements.fetch(_1) }}
.map { score(_1) } .map { score(_1) }
p scores.sort.fetch(scores.length / 2) p scores.sort.fetch(scores.length / 2)

Loading…
Cancel
Save