Created
September 21, 2022 03:57
-
-
Save msakai/26c644cac13d9b16da36ca39455294fb to your computer and use it in GitHub Desktop.
My solution for "Cookpad Code Puzzle for RubyKaigi 2022" https://ruby-puzzles-2022.cookpad.tech/
This file contains 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
# coding: utf-8 | |
def answer1(n) | |
n + 1 | |
end | |
def answer2(str) | |
str.upcase | |
end | |
def answer3(n) | |
(0..n).to_a | |
end | |
def answer4 | |
x = yield | |
x + 42 | |
end | |
def answer5(x, *args) | |
if args.empty? | |
x + 1 | |
else | |
x + args.sum | |
end | |
end | |
def answer6(n) | |
while n >= 10 do | |
n = n.to_s.chars.map{|c| c.to_i }.sum | |
end | |
n | |
end | |
def answer7(n) | |
n.succ | |
end | |
def answer8(n) | |
format("%b", n).chars.select{|c| c == '1' }.size | |
end | |
def answer9(s) | |
s.gsub("u-g0t-me", "yikes") | |
end | |
$state10 = 0 | |
def answer10(reset=false) | |
if reset | |
$state10 = 0 | |
else | |
$state10 += 1 | |
end | |
end | |
# localStorage.stage = 20 | |
def answer11(n) | |
n.hash | |
end | |
def answer12(n) | |
h = Hash.new(0) | |
n.to_s.chars.each{|c| h[c] += 1 } | |
h.values.inject(1){|a,b| a*b} | |
end | |
def answer13(n) | |
e = Encoding.list[n] | |
if e.nil? | |
nil | |
else | |
e.name[0..1] | |
end | |
end | |
def answer14(n) | |
if n == 1000 | |
"T" | |
elsif n < 20 | |
"ZOTTFFSSENTETTFFSSEN"[n] | |
else | |
"ZOTTFFSSEN"[n.to_s[0].to_i] | |
end | |
end | |
def answer15(n) | |
x = Object.constants.map{|x| x.to_s}.sort[n] | |
if x.nil? | |
x | |
else | |
x[0..1] | |
end | |
end | |
def answer16(&blk) | |
flag = false | |
blk.call { flag = true } | |
flag | |
end | |
def answer17(s) | |
JS.eval("alert(\"#{s}\")") | |
end | |
def answer18(s) | |
ret = [] | |
s.scan(/\G\s*(?:(\d+)|([+\-*\/()])|(\z)|(.))/m) { | |
if $1 | |
ret << $1.to_i | |
elsif $2 | |
ret << $2 | |
elsif $4 | |
ret << $4 | |
end | |
} | |
ret | |
end | |
def parse_expr(ts) | |
lhs = parse_term(ts) | |
while ts.size > 0 && (ts[0] == '+' || ts[0] == '-') | |
op = ts.shift | |
rhs = parse_term(ts) | |
lhs = [op, lhs, rhs] | |
end | |
lhs | |
end | |
def parse_term(ts) | |
lhs = parse_factor(ts) | |
while ts[0] == "*" || ts[0] == "/" | |
op = ts.shift | |
rhs = parse_factor(ts) | |
lhs = [op, lhs, rhs] | |
end | |
lhs | |
end | |
def parse_factor(ts) | |
if ts[0] == '(' | |
ts.shift | |
ret = parse_expr(ts) | |
ts.shift | |
ret | |
else | |
["value", ts.shift] | |
end | |
end | |
def answer19(ts) | |
parse_expr(ts) | |
end | |
def answer20(e) | |
case e | |
in ["value", x] | |
x | |
in ["+", x, y] | |
answer20(x) + answer20(y) | |
in ["-", x, y] | |
answer20(x) - answer20(y) | |
in ["*", x, y] | |
answer20(x) * answer20(y) | |
in ["/", x, y] | |
answer20(x) / answer20(y) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment