Skip to content

Instantly share code, notes, and snippets.

@tkm-kj
Created December 27, 2015 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkm-kj/e9b66d057307a9e146b1 to your computer and use it in GitHub Desktop.
Save tkm-kj/e9b66d057307a9e146b1 to your computer and use it in GitHub Desktop.
第2回 ESMオフラインどう書く(その2)
class Star
BASE = 'ABCDEFGHIJ'.chars
LINES = [
['A', 'F', 'G', 'D'],
['B', 'J', 'F', 'E'],
['C', 'I', 'J', 'A'],
['D', 'H', 'I', 'B'],
['E', 'G', 'H', 'C']
]
def initialize(sample)
@sample = sample
@converted_base = 'WWWWWRRRRR'.chars
end
def solve
@sample.chars.each do |char|
change!(base_index(char))
LINES.each do |line|
if i = line.index(char)
i / (line.size / 2) < 1 ? check_sequence(i, i + 1, line) : check_sequence(i, i - 1, line)
end
end
end
@converted_base.join
end
private
def check_sequence(current, other, line)
if current - other < 0
next_index, end_of_line = 1, line.size - 1
else
next_index, end_of_line = -1, 0
end
if @converted_base[base_index(line[current])] == @converted_base[base_index(line[other])]
unless (current - other).abs == 1
target = current + next_index
until target == other
change!(base_index(line[target])) unless @converted_base[base_index(line[current])] == @converted_base[base_index(line[target])]
target += next_index
end
else
return
end
end
other == end_of_line ? return : check_sequence(current, other + next_index, line)
end
def base_index(char)
BASE.index(char)
end
def change!(index)
@converted_base[index] = @converted_base[index] == 'R' ? 'W' : 'R'
end
end
def test(sample, result)
p Star.new(sample).solve == result
end
test("A", "RWWWWRRRRR")
test("F", "WWWWWWWRRW")
test("J", "WWWWWWRRWW")
test("AA", "WWWWWWWRWW")
test("IC", "WWRWWRRRWW")
test("FC", "WWRWWWWRRW")
test("AE", "RWWWRRRRRR")
test("GJ", "WWWWWWWWWW")
test("CCB", "WRWWWRWWWR")
test("BEF", "WRWWRWWRRR")
test("JGD", "WWWRWWWWWW")
test("IHCC", "WWWWWRWWWW")
test("AIDD", "RWWWWRRWWR")
test("IJFA", "RWWWWWWWWW")
test("ABCDE", "RRRRRRRRRR")
test("ICEBA", "RRRWRRRRRR")
test("DAHHD", "RWWWWRWWWR")
test("GJIJC", "WWRWWWWWRR")
test("FGHIJ", "WWWWWWWWRR")
test("HJICGA", "RWRWWRRRRR")
test("IBCIGC", "WRWWWWWWWW")
test("BIJJJB", "WWWWWWRWWW")
test("DCBCHGD", "WRWWWWWRRW")
test("JEABDHD", "RRWWRRRWRR")
test("JHFADHE", "RWWRRRRRWW")
test("HDGGDBIB", "WWWWWWWWWW")
test("IIDIHCCG", "WWWRWRRWWW")
test("BBFBICIE", "WRRWRRRWWW")
test("HJHCFBJGG", "WRRWWWWRRW")
test("AJJIEAAII", "RWWWRWWWWR")
test("AIDHJFGAE", "WWWRRWWWWW")
test("FGBGHCBHJJ", "WWRWWWWRRW")
test("EFIGIGGHHJ", "WWWWRRRWWR")
test("HGAFDIFFFF", "RWWRWRRRRW")
test("AABBCCDDEE", "WWWWWWWWWW")
test("ABCDEFGHIJ", "RRRRRWWWWW")
test("FGHIJABCDE", "RRRRRRRRRR")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment