[RUBY] Bit Tetris (how to write)

http://nabetani.sakura.ne.jp/hena/ord2/

The problem is to erase a line of Tetris represented by bits.

Ruby


module BitTetris
  module_function
  
  def solve(input)    
    field = input.split("-").map { _1.to_i(16) }
    to_be_erased = to_a(field.inject(:&))
    field.map { "%02x" % erase(_1, to_be_erased) }.join("-")
  end
  
  def erase(i, to_be_erased)
    to_a(i).zip(to_be_erased).map { |s, t| t == "1" ? "" : s }.join.to_i(2)
  end
  
  def to_a(i)
    sprintf("%08b", i).chars
  end
end


if __FILE__ == $0
  require 'minitest/autorun'
  describe 'BitTetris' do
    [
      ["ff-2f-23-f3-77-7f-3b", "1f-03-00-1c-0d-0f-06"],
      ["01", "00"],
      ["00", "00"],
      ["7a-4e", "0c-02"],
      ["56-b6", "08-14"],
      ["12-12-12", "00-00-00"],
      ["de-ff-7b", "0a-0f-05"],
      ["95-be-d0", "05-1e-20"],
      ["7c-b0-bb", "1c-20-2b"],
      ["7a-b6-31-6a", "3a-56-11-2a"],
      ["32-0e-23-82", "18-06-11-40"],
      ["ff-7f-bf-df-ef", "0f-07-0b-0d-0e"],
      ["75-df-dc-6e-42", "35-5f-5c-2e-02"],
      ["62-51-ef-c7-f8", "22-11-6f-47-78"],
      ["0c-47-8e-dd-5d-17", "04-23-46-6d-2d-0b"],
      ["aa-58-5b-6d-9f-1f", "52-28-2b-35-4f-0f"],
      ["ff-55-d5-75-5d-57", "0f-00-08-04-02-01"],
      ["fe-fd-fb-f7-ef-df-bf", "7e-7d-7b-77-6f-5f-3f"],
      ["fd-fb-f7-ef-df-bf-7f", "7e-7d-7b-77-6f-5f-3f"],
      ["d9-15-b5-d7-1b-9f-de", "69-05-55-67-0b-4f-6e"],
      ["38-15-fd-50-10-96-ba", "18-05-7d-20-00-46-5a"],
      ["fe-fd-fb-f7-ef-df-bf-7f", "fe-fd-fb-f7-ef-df-bf-7f"]
    ].each do |input, expect|
      it input do
        assert_equal BitTetris.solve(input), expect 
      end
    end
  end
end

First, find the row to be erased by bit operation (field.inject (: &)), and then erase the bits to be erased in each column with the erase method. I was wondering what to do to erase the bit. After all, after expressing it in binary, I converted it to Array (to_a method) and erased it with map. I think it's a matter of how to use the binary and hexadecimal representations of String and Integer properly.

In addition, it should be notederaseI'm doing one liner with the methodary1.zip(ary2).map {|a, b| ...}I often write something like thiszipRecreating the Array in is not efficient, but it can be succinct. If you do it honestly (?), Is it like this?

  def erase(i, to_be_erased)
    a = to_a(i)
    result = ""
    a.length.times do |j|
      result << a[j] if to_be_erased[j] != "1"
    end
    result.to_i(2)
  end

This should be faster and more efficient.

Recommended Posts

Bit Tetris (how to write)
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
How to write good code
How to write java comments
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to write Rails routing
Studying Java # 6 (How to write blocks)
Baseball ball count (how to write)
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
[Basic] How to write a Dockerfile Self-learning ②
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
How to deploy
[Java] How to output and write files!
How to write Spring AOP pointcut specifier
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
JDBC promises and examples of how to write
Rails: How to write a rake task nicely
[JavaFX] How to write Eclipse permissions in build.gradle
How to write offline 15th reference question answer
[Rails] How to write when making a subquery
Java Development Basics ~ How to Write Programs * Exercise 1 ~
How to write an if statement to improve readability-java
JUnit 5: How to write test cases in enum
Offline real-time how to write F06 implementation example
How to write code that thinks object-oriented Ruby
How to write test code with Basic authentication
How to write React Native bridge ~ Android version ~
[Java] Memo on how to write the source
How to write Java String # getBytes in Kotlin?
How to run NullpoMino 7.5.0 on Ubuntu 20.04.1 64bit version
Notes on how to write comments in English
How to develop OpenSPIFe
How to call AmazonSQSAsync
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
Introduction to bit operation
How to use map
How to use collection_select