29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

愛を生む二人を探して

Last updated at Posted at 2016-05-09

なんで愛が生まれるのか を読んで面白いと思ったので・・・。

UTF-8 で漢字は概ね U+0800 - U+FFFF の範囲に収まるようです。本当はいろいろあるようですが、今回はとりあえずこの範囲で生成します。 Wikipedia によれば、この範囲の意味のあるビットは 1110xxxx 10xxxxxx 10xxxxxx の x の部分。このうち「愛」で 0 になっているビット(下記太字)を 0 & 0, 0 & 1, 1 & 0 に変えた組み合わせは、愛を生むはずです。

文字 バイナリ
1 1 1 0 0 1 1 1 1 0 0 1 0 1 0 0 1 0 0 1 1 1 1 1
1 1 1 0 0 1 1 0 1 0 1 0 1 1 0 1 1 0 1 1 1 0 1 1
1 1 1 0 0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 1 1 0 1 1

ということで Ruby で書いてみました。ビットだけ見て「愛」を生み出すカップルを探した後、漢字じゃないものと「愛」そのものを除外します。

結果は Gist に。3025 組のカップルが成立しました!「生」と「死」もしっかり入ってます。めでたしめでたし。

couple.rb
# Extract meaningful bits.
# https://en.wikipedia.org/wiki/UTF-8#Description
def code_bits(bits)
  bits.each_slice(8).with_index.map do |byte, i|
    i == 0 ? byte[4..-1] : byte[2..-1]
  end.flatten
end

# Returns unicode charactor from bits.
def bits_to_char(bits)
  size = bits.size
  code = bits.each.with_index.reduce(0) do |acc, pair|
    bit, i = pair
    acc + (bit << size - 1 - i)
  end
  [code].pack('U')
end

# Generate possible bits pairs.
def generate(bits)
  if bits.size == 0
    [
      [[], []]
    ]
  elsif bits[0] == 0
    generate(bits[1..-1]).flat_map do |pair|
      a, b = pair
      # 0 can be generated from 0 & 0, 0 & 1 and 1 & 0.
      [
        [[0] + a, [0] + b],
        [[0] + a, [1] + b],
        [[1] + a, [0] + b]
      ]
    end
  else
    generate(bits[1..-1]).map do |pair|
      a, b = pair
      # 1 is generated only from 1 & 1.
      [[1] + a, [1] + b]
    end
  end
end

# http://qiita.com/tienlen/items/44daf3a52eb872085556
ai = %w(1 1 1 0 0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 1 1 0 1 1).map(&:to_i)
ai_bits = code_bits(ai)

ai_pairs = generate(ai_bits).map do |pair|
  pair.map do |bits|
    bits_to_char(bits)
  end.sort
end

kanji_pairs = ai_pairs.select do |pair|
  pair.all? do |char|
    # http://tama-san.com/kanji-regex/
    # char != '愛' && /\p{Han}/ =~ char
    char != '愛' && /[一-龠]/ =~ char
  end
end.uniq

kanji_pairs.each do |pair|
  puts pair.join(',')
end
29
27
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?