3
2

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.

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

Posted at

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

解説は近いうちに…。

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
3
2
0

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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?