Skip to content

Instantly share code, notes, and snippets.

@kunitoo
Last active December 26, 2015 08:08
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 kunitoo/d1b32d487553bf3a04fd to your computer and use it in GitHub Desktop.
Save kunitoo/d1b32d487553bf3a04fd to your computer and use it in GitHub Desktop.
瞬き星 〜 第2回 ESM オフラインどう書く (2015/12/24) https://gist.github.com/mattsan/07674b095908fda117a0
require 'rubygems'
class Node
attr :name
attr_accessor :color
def initialize(name, color)
@name = name
@color = color
end
def toggle
@color = @color == 'W' ? 'R' : 'W'
end
end
class Star
attr :nodes
def initialize
pairs = ('A'..'J').zip('WWWWWRRRRR'.chars)
@nodes = pairs.map {|pair|
Node.new(*pair)
}
@routes = [
%w(A J I C),
%w(A F G D),
%w(B J F E),
%w(B I H D),
%w(C H G E)
]
end
def toggle(name)
node = find_node(name)
node.toggle
target_routes = @routes.select {|r| r.include?(name) }
target_routes.each {|route|
toggle_route(node, route)
}
end
def toggle_route(node, route)
case route.index(node.name)
when 0
if find_node(route[3]).color == node.color && find_node(route[1]).color != node.color
[1, 2].each do |index|
find_node(route[index]).color = node.color
end
elsif find_node(route[2]).color == node.color
find_node(route[1]).color = node.color
end
when 1
if find_node(route[3]).color == node.color
find_node(route[2]).color = node.color
end
when 2
if find_node(route[0]).color == node.color
find_node(route[1]).color = node.color
end
when 3
if find_node(route[0]).color == node.color && find_node(route[2]).color != node.color
[1, 2].each do |index|
find_node(route[index]).color = node.color
end
elsif find_node(route[1]).color == node.color
find_node(route[2]).color = node.color
end
end
end
def find_node(name)
@nodes.find {|n| n.name == name }
end
end
def setup
Star.new
end
def resolve(input)
star = setup
input.chars.each do |name|
star.toggle(name)
end
star.nodes.map(&:color).join
end
def test(input, expected)
result = resolve(input)
if result == expected
print '.'
else
puts
puts %Q[test("#{input}", "#{expected}") #=> got #{result}]
end
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")
@kunitoo
Copy link
Author

kunitoo commented Dec 24, 2015

時間内に解けなかった...

@kunitoo
Copy link
Author

kunitoo commented Dec 26, 2015

その後 test("BBFBICIE", "WRRWRRRWWW") 以外通るようになり、しばらくしたあとに全部通すことができました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment