AtCoder Beginner Contest D - Lamp Difficulty: 1103
This theme, transpiler
This problem was previously solved with AtCoder ABC129 D 2D Array in Ruby and Java. It's a speedy issue for Ruby.
This time, when I manually transpiled the Ruby code to the Crystal code, it improved to 1281 ms
-> 227 ms
. I hope it will be automatically transpiled in the future.
Ruby -> JavaScript
Speaking of transpilers in Ruby, Opal
While the publication of Ruby-related books is lonely, "Practice Opal" was published in May 2020.
However, in competition pros, the speed improvement is considered to be small (because of scripting language-> scripting language). ~~ I'm glad if something goes wrong and it becomes Ruby-> Java ~~
Ruby -> C
If you are developing Ruby, you are a master of Ruby
and C
, so can you do it easily?
Ruby -> Go
Google secretly develops it
Ruby -> Crystal
Crystal
Crysta.cr
h, w = read_line.split.map { |c| c.to_i }
s = [] of Array(UInt8)
h.times do
s << read_line.chomp.bytes
end
hs = [] of Array(Int32)
h.times do |y|
cnt = 0
v = [] of Int32
w.times do |x|
if s[y][x] == 46
cnt += 1
else
cnt.times do
v << cnt
end
cnt = 0
v << 0
end
end
cnt.times do
v << cnt
end
hs << v
end
vs = [] of Array(Int32)
w.times do |x|
cnt = 0
v = [] of Int32
h.times do |y|
if s[y][x] == 46
cnt += 1
else
cnt.times do
v << cnt
end
cnt = 0
v << 0
end
end
cnt.times do
v << cnt
end
vs << v
end
max = 0
h.times do |i|
w.times do |j|
max = hs[i][j] + vs[j][i] if max < hs[i][j] + vs[j][i]
end
end
puts max - 1
Array.cr
h, w = read_line.split.map { |c| c.to_i }
s = [] of Array(UInt8)
It doesn't feel strange, just the standard input and array initialization have changed. Ruby
Ruby.rb
h, w = gets.split.map(&:to_i)
s = h.times.map { gets.chomp.bytes }
hs = []
s.each do |t|
cnt = 0
v = []
t.each do |x|
if x == 46
cnt += 1
else
cnt.times do
v << cnt
end
cnt = 0
v << 0
end
end
cnt.times do
v << cnt
end
hs << v
end
vs = []
s.transpose.each do |t|
cnt = 0
v = []
t.each do |x|
if x == 46
cnt += 1
else
cnt.times do
v << cnt
end
cnt = 0
v << 0
end
end
cnt.times do
v << cnt
end
vs << v
end
max = 0
h.times do |i|
w.times do |j|
max = hs[i][j] + vs[j][i] if max < hs[i][j] + vs[j][i]
end
end
puts max - 1
This is the original code.
Ruby 2.7.1 | Crystal 0.33.0 | |
---|---|---|
Code length(Byte) | 710 | 811 |
Execution time(ms) | 1281 | 227 |
memory(KB) | 158448 | 58176 |
It's about 5 times faster.
Recommended Posts