Created
December 24, 2015 07:18
-
-
Save hidenba/8734179e4d15168dd461 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point | |
attr_accessor :point, :color | |
def initialize(point, color) | |
@point = point | |
@color = color | |
end | |
def toggle | |
@color = color == 'R' ? 'W' : 'R' | |
end | |
end | |
class Star | |
attr_reader :points | |
LINES = %w(AJIC AFGD BJFE BIHD CHGE) | |
def initialize | |
@points = {'A': Point.new('A', 'W'), | |
'B': Point.new('B', 'W'), | |
'C': Point.new('C', 'W'), | |
'D': Point.new('D', 'W'), | |
'E': Point.new('E', 'W'), | |
'F': Point.new('F', 'R'), | |
'G': Point.new('G', 'R'), | |
'H': Point.new('H', 'R'), | |
'I': Point.new('I', 'R'), | |
'J': Point.new('J', 'R') | |
} | |
end | |
def trace(route) | |
routes = route.split(//) | |
routes.each do |point| | |
base = points[point.to_sym] | |
base.toggle | |
LINES.each do |line| | |
base_index = line.index(base.point) | |
if base_index | |
if base_index >= 2 | |
base_index = line.reverse.index(base.point) | |
traces = line.reverse.split(//).map { |key| points[key.to_sym]} | |
else | |
traces = line.split(//).map { |key| points[key.to_sym]} | |
end | |
if !traces[base_index+2].nil? && traces[base_index+2].color == base.color && traces[base_index+1].color != base.color | |
traces[base_index+1].toggle | |
elsif !traces[base_index+3].nil? && traces[base_index+3].color == base.color && traces[base_index+1].color != base.color && traces[base_index+2].color != base.color | |
traces[base_index+1].toggle | |
traces[base_index+2].toggle | |
end | |
end | |
end | |
end | |
points.map { |_k, v| v.color }.join('') | |
end | |
end | |
def test(route, result) | |
traced = Star.new.trace(route) | |
puts "#{traced}: #{traced == 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