Help us understand the problem. What is going on with this article?

オフラインリアルタイムどう書くE04 をビット演算で解いた

More than 3 years have passed since last update.

鍋谷さんの解法が綺麗でなんか悔しかったので、別解を模索しました。
結果、頂上から麓へ向けて横道を一回走査するだけで答えを算出できる方法を見つけました。
せっかくなのでビット演算で実装しています。

解説は近いうちに…。

orde04.rb
def solve(input)
  brahces, rock = ->(bs, r) { [bs.chars.map {|b| (0x181 >> (8 - b.to_i)) & 0xff }, 1 << (r.ord - ?A.ord)] }[*input.split(':')]

  death_route = brahces.reduce(rock) {|death_route, branch|
    if (rock & branch) != 0
      rock ^= branch
      death_route | branch
    else
      mask = branch
      mask ^= branch if ((death_route ^ branch) & branch) * (death_route & branch) == 0
      death_route ^ mask
    end
  }

  ('A'..'H').map.with_index {|c, i| c if death_route[i] == 0 }.join
end

def test(input, expected)
  actual = solve(input)

  if actual == expected
    print "\x1b[32m.\x1b[0m"
  else
    puts <<~EOS
      \x1b[31m
      input:    #{input}
      expected: #{expected}
      actual:   #{actual}\x1b[0m
    EOS
  end
end

test('2512:C', 'DEFGH')
test('1:A', 'CDEFGH')
test(':C', 'ABDEFGH')
test('2345:B', 'AGH')
test('1256:E', 'ABCDH')
test('1228:A', 'ADEFG')
test('5623:B', 'AEFGH')
test('8157:C', 'ABDEFGH')
test('74767:E', 'ABCFGH')
test('88717:D', 'ABCEFGH')
test('148647:A', 'ACDEFH')
test('374258:H', 'BCDEFH')
test('6647768:F', 'ABCDEH')
test('4786317:E', 'ABFGH')
test('3456781:C', '')
test('225721686547123:C', 'CEF')
test('2765356148824666:F', 'ABCDEH')
test('42318287535641783:F', 'BDE')
test('584423584751745261:D', 'FGH')
test('8811873415472513884:D', 'CFG')
test('74817442725737422451:H', 'BCDEF')
test('223188865746766511566:C', 'ABGH')
test('2763666483242552567747:F', 'ABCG')
test('76724442325377753577138:E', 'EG')
test('327328486656448784712618:B', '')
test('4884637666662548114774288:D', 'DGH')
test('84226765313786654637511248:H', 'DEF')
test('486142154163288126476238756:A', 'CDF')
test('1836275732415226326155464567:F', 'BCD')
test('62544434452376661746517374245:G', 'G')
test('381352782758218463842725673473:B', 'A')
puts
emattsan
ソフトウェアエンジニア、言語オタク
http://d.hatena.ne.jp/E_Mattsan/
esm
We build applications which work well and make customers happy.
http://www.esm.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした