Solving with Ruby and Crystal AtCoder ABC 129 D

Introduction

This theme

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.

Summary

Recommended Posts

Solving with Ruby and Crystal AtCoder ABC 129 D
Solving with Ruby, Perl and Java AtCoder ABC 136 D Breadth-first search
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Solve with Ruby AtCoder ABC177 D UnionFind
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
AtCoder ABC127 D hash to solve with Ruby 2.7.1
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming
atcoder ABC70 D problem
[At Coder] Solve the ABC183 D problem with Ruby
[At Coder] Solve the ABC182 D problem with Ruby
ABC177 --solving E in Ruby
Solving with Ruby AtCoder 1st Algorithm Practical Test A Exception Handling
With ruby ● × Game and Othello (basic review)
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
Convert JSON to TSV and TSV to JSON with Ruby
[Ruby] Arguments with keywords and default values of arguments
I implemented Ruby with Ruby (and C) (I played with builtin)
AtCoder ABC 169 C Floating Point Fits in Ruby
Try to link Ruby and Java with Dapr
Create jupyter notebook with Docker and run ruby
Ruby and Gem
Feel the basic type and reference type easily with ruby
[Ruby] Exclude and replace specific patterns with regular expressions
[Ruby] I made a crawler with anemone and nokogiri.
Feel the basic type and reference type easily with ruby 2
Install rbenv with apt on ubuntu and put ruby