Last active
December 26, 2015 08:08
-
-
Save kunitoo/d1b32d487553bf3a04fd to your computer and use it in GitHub Desktop.
瞬き星 〜 第2回 ESM オフラインどう書く (2015/12/24) https://gist.github.com/mattsan/07674b095908fda117a0
This file contains 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
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") |
その後 test("BBFBICIE", "WRRWRRRWWW")
以外通るようになり、しばらくしたあとに全部通すことができました。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
時間内に解けなかった...