Skip to content

Instantly share code, notes, and snippets.

@Akiyah
Last active May 4, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akiyah/dc344d02af97d63a4e37394103e57bd2 to your computer and use it in GitHub Desktop.
Save Akiyah/dc344d02af97d63a4e37394103e57bd2 to your computer and use it in GitHub Desktop.
Solver of RubyKaigi 2019 Cookpad Daily Ruby Puzzles
Example
2019-05-04 02:31:22 +0900
...........................
-----
def foo
"Hello world" if%
false
end
puts foo
-----
.
-----
def foo
"Hello world" if
! false
end
puts foo
-----
.
-----
def foo
"Hello world" if
! false
end
puts foo
-----
.
-----
def foo
"Hello world" if
! false
end
puts foo
-----
.
-----
def foo
"Hello world" if
! false
end
puts foo
-----
.
-----
def foo
"Hello world" if
! false
end
puts foo
-----
.
-----
def foo
"Hello world" if
! false
end
puts foo
-----
.
-----
def foo
"Hello world" if
! false
end
puts foo
-----
.
-----
def foo
"Hello world" if
!false
end
puts foo
-----
-----
def foo
"Hello world" if
:false
end
puts foo
-----
..................
2019-05-04 02:44:11 +0900
Problem 1-1
2019-05-04 02:44:11 +0900
..........................................
-----
# Hint: Use Ruby 2.6
puts "#{"Goodbye" ..; "Hello"} world"
-----
.
-----
# Hint: Use Ruby 2.6
puts "#{"Goodbye" .. ;"Hello"} world"
-----
...............
2019-05-04 02:57:01 +0900
Problem 1-2
2019-05-04 02:57:01 +0900
.....
-----
puts$&.then {
# Hint: &. is a safe
# navigation operator
"Hello world"
}
-----
.........................................................................
2019-05-04 03:14:12 +0900
Problem 1-3
2019-05-04 03:14:12 +0900
...................................................................................................
-----
include Math
# Hint: the most beautiful equation
Out, *,
Count = $>,
$<, E ** (2i * PI)
Out.puts("Hello world" *
Count.abs.round)
-----
.........................................................
2019-05-04 03:48:55 +0900
Problem 2-1
2019-05-04 03:48:55 +0900
.....................................
-----
def say
-> {
"Hello world"
}.
# Hint: you should call the Proc.
yield
end
puts say { "Goodbye world" }
-----
.
-----
def say
-> {
"Hello world"
}
. # Hint: you should call the Proc.
yield
end
puts say { "Goodbye world" }
-----
.
-----
def say
-> {
"Hello world"
}
. # Hint: you should call the Proc.
yield
end
puts say { "Goodbye world" }
-----
.
-----
def say
-> {
"Hello world"
}
.# Hint: you should call the Proc.
yield
end
puts say { "Goodbye world" }
-----
...........................................................................
2019-05-04 04:14:21 +0900
Problem 2-2
2019-05-04 04:14:21 +0900
......................................................
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
#
essentially Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
#
essentially Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# e
ssentially Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# es
sentially Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# ess
entially Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# esse
ntially Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# essen
tially Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# essent
ially Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# essenti
ally Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# essentia
lly Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# essential
ly Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# essentiall
y Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# essentially
Fiber.
yield "Hello world"
end
puts e.next
-----
.
-----
e = Enumerator.new do |g|
# Hint: Enumerator is
# essentially
Fiber.
yield "Hello world"
end
puts e.next
-----
.............................................
2019-05-04 04:39:07 +0900
Problem 2-3
2019-05-04 04:39:07 +0900
......................................................................
-----
$s = 0
def say(n = 0)
$s = $s * 4 + n
end
i, j, k = 1, 2, 3
say if
say j
say k
# Hint: Binary representation.
$s != 35 or puts("Hello world")
-----
............................................................................
2019-05-04 05:11:20 +0900
Problem 3-1
2019-05-04 05:11:20 +0900
..............................................................................................................
-----
def say s="Hello", t:'world'
"#{ s }#{ t } world"
end
# Hint: Arguments in Ruby are
# difficult.
puts say t:p
-----
..
2019-05-04 05:36:03 +0900
Problem 3-2
2019-05-04 05:36:03 +0900
..............
-----
def say s, t=#"Goodbye "
# Hint: You can ignore a warning.
s = "#{ s } #{ t }"
t + "world"
end
puts say :Hello
-----
.......................................................................................................
2019-05-04 06:01:55 +0900
Problem 3-3
2019-05-04 06:01:55 +0900
...........................
-----
def say
"Hello world" if%
false && false
# Hint: No hint!
end
puts say
-----
....................................................
2019-05-04 06:19:22 +0900
Extra 1
2019-05-04 06:19:22 +0900
...............................................................
-----
Hello = "Hello"
# Hint: Stop the recursion.
def Hello
Hello%() +
" world"
end
puts Hello()
-----
...................................
2019-05-04 06:41:46 +0900
Extra 2
2019-05-04 06:41:46 +0900
......
-----
s = "Dz"
# Hint: https://techlife.cookpad.com/entry/2018/12/25/110240
s == s.upcase or
s == s.downcase or puts "Hello world"
-----
.......................................................................................................................
2019-05-04 07:09:22 +0900
Extra 3
2019-05-04 07:09:22 +0900
........
-----
def say\
s = 'Small'
t = 'world'
puts "#{s} #{t}"
end
TracePoint.new(:line){|tp|
tp.binding.local_variable_set(:s, 'Hello')
tp.binding.local_variable_set(:t, 'Ruby')
tp.disable
}.enable(target: method(:say))
say
-----
.....
-----
def say
s %= 'Small'
t = 'world'
puts "#{s} #{t}"
end
TracePoint.new(:line){|tp|
tp.binding.local_variable_set(:s, 'Hello')
tp.binding.local_variable_set(:t, 'Ruby')
tp.disable
}.enable(target: method(:say))
say
-----
....................................................................................................................................................................................................................
2019-05-04 07:59:10 +0900
CODES = {}
CODES["Example"] = <<'EOS'
def foo
"Hello world" if
false
end
puts foo
EOS
CODES["Problem 1-1"] = <<'EOS'
# Hint: Use Ruby 2.6
puts "#{"Goodbye" .. "Hello"} world"
EOS
CODES["Problem 1-2"] = <<'EOS'
puts&.then {
# Hint: &. is a safe
# navigation operator
"Hello world"
}
EOS
CODES["Problem 1-3"] = <<'EOS'
include Math
# Hint: the most beautiful equation
Out, *,
Count = $>,
$<, E ** (2 * PI)
Out.puts("Hello world" *
Count.abs.round)
EOS
CODES["Problem 2-1"] = <<'EOS'
def say
-> {
"Hello world"
}
# Hint: you should call the Proc.
yield
end
puts say { "Goodbye world" }
EOS
CODES["Problem 2-2"] = <<'EOS'
e = Enumerator.new do |g|
# Hint: Enumerator is
# essentially Fiber.
yield "Hello world"
end
puts e.next
EOS
CODES["Problem 2-3"] = <<'EOS'
$s = 0
def say(n = 0)
$s = $s * 4 + n
end
i, j, k = 1, 2, 3
say i
say j
say k
# Hint: Binary representation.
$s != 35 or puts("Hello world")
EOS
CODES["Problem 3-1"] = <<'EOS'
def say s="Hello", t:'world'
"#{ s }#{ t } world"
end
# Hint: Arguments in Ruby are
# difficult.
puts say :p
EOS
CODES["Problem 3-2"] = <<'EOS'
def say s, t="Goodbye "
# Hint: You can ignore a warning.
s = "#{ s } #{ t }"
t + "world"
end
puts say :Hello
EOS
CODES["Problem 3-3"] = <<'EOS'
def say
"Hello world" if
false && false
# Hint: No hint!
end
puts say
EOS
CODES["Extra 1"] = <<'EOS'
Hello = "Hello"
# Hint: Stop the recursion.
def Hello
Hello() +
" world"
end
puts Hello()
EOS
CODES["Extra 2"] = <<'EOS'
s = ""
# Hint: https://techlife.cookpad.com/entry/2018/12/25/110240
s == s.upcase or
s == s.downcase or puts "Hello world"
EOS
CODES["Extra 3"] = <<'EOS'
def say
s = 'Small'
t = 'world'
puts "#{s} #{t}"
end
TracePoint.new(:line){|tp|
tp.binding.local_variable_set(:s, 'Hello')
tp.binding.local_variable_set(:t, 'Ruby')
tp.disable
}.enable(target: method(:say))
say
EOS
require 'timeout'
CHARS =
('0'..'9').to_a +
('a'..'z').to_a +
('A'..'Z').to_a +
%w( ! ? # % & | + - * / ^ ' . , < > = ~ $ @ _ " : ` \\ ; ) +
[' ', "\n", "Dz"]
CODES.each do |name, code|
puts
puts "#{name}"
puts Time.now
(0...code.length).each do |i|
putc '.'
CHARS.each do |char|
code_inserted = code.clone.insert(i, char)
File.open("code.rb", "w") { |f| f.puts(code_inserted) }
begin
Timeout.timeout(1) do
result = `ruby code.rb 2>/dev/null`
if result == "Hello world\n"
puts
puts '-----'
puts code_inserted
puts '-----'
end
end
rescue Timeout::Error
end
end
end
puts
puts Time.now
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment