Created
December 24, 2015 07:21
-
-
Save alpaca-tc/77721987aa32f0191a5b 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
require 'pry' | |
class FlashStar | |
RELATION = {# {{{ | |
A: [ | |
%i(B), | |
%i(E), | |
%i(J I C), | |
%i(F G D), | |
], | |
B: [ | |
%i(A), | |
%i(J F E), | |
%i(I H D), | |
%i(C) | |
], | |
C: [ | |
%i(B), | |
%i(I J A), | |
%i(H G E), | |
%i(D) | |
], | |
D: [ | |
%i(H I B), | |
%i(G F A), | |
%i(E) | |
], | |
E: [ | |
%i(A), | |
%i(F J B), | |
%i(G H C), | |
%i(D) | |
], | |
F: [ | |
%i(E), | |
%i(A), | |
%i(J B), | |
%i(G D) | |
], | |
G: [ | |
%i(F A), | |
%i(E), | |
%i(D), | |
%i(H C) | |
], | |
H: [ | |
%i(G E), | |
%i(D), | |
%i(C), | |
%i(I B) | |
], | |
I: [ | |
%i(J A), | |
%i(H D), | |
%i(C), | |
%i(B) | |
], | |
J: [ | |
%i(A), | |
%i(F E), | |
%i(I C), | |
%i(B) | |
] | |
}.freeze# }}} | |
def emit(label) | |
to_h[label] = !to_h[label] | |
propagation_changed(label) | |
end | |
def to_h | |
@to_a ||= { | |
A: false, | |
B: false, | |
C: false, | |
D: false, | |
E: false, | |
F: true, | |
G: true, | |
H: true, | |
I: true, | |
J: true | |
} | |
end | |
def to_s | |
to_h.values.map { |bool| bool ? 'R' : 'W' }.join | |
end | |
private | |
def propagation_changed(label) | |
relations = RELATION[label] | |
current = to_h[label] | |
relations.each do |related_labels| | |
check_related_labels(label, related_labels, current) | |
end | |
inspect | |
end | |
def check_related_labels(label, related_labels, current) | |
related_labels.each_with_index do |related_label, index| | |
next if index.zero? | |
if to_h[related_label] == current | |
related_labels[0...index].each do |_label| | |
to_h[_label] = current | |
end | |
return | |
end | |
end | |
end | |
end | |
expected_result = [ | |
['A', 'RWWWWRRRRR'], | |
['F', 'WWWWWWWRRW'], | |
['J', 'WWWWWWRRWW'], | |
['AA', 'WWWWWWWRWW'], | |
['IC', 'WWRWWRRRWW'], | |
['FC', 'WWRWWWWRRW'], | |
['AE', 'RWWWRRRRRR'], | |
['GJ', 'WWWWWWWWWW'], | |
['CCB', 'WRWWWRWWWR'], | |
['BEF', 'WRWWRWWRRR'], | |
['JGD', 'WWWRWWWWWW'], | |
['IHCC', 'WWWWWRWWWW'], | |
['AIDD', 'RWWWWRRWWR'], | |
['IJFA', 'RWWWWWWWWW'], | |
['ABCDE', 'RRRRRRRRRR'], | |
['ICEBA', 'RRRWRRRRRR'], | |
['DAHHD', 'RWWWWRWWWR'], | |
['GJIJC', 'WWRWWWWWRR'], | |
['FGHIJ', 'WWWWWWWWRR'], | |
['HJICGA', 'RWRWWRRRRR'], | |
['IBCIGC', 'WRWWWWWWWW'], | |
['BIJJJB', 'WWWWWWRWWW'], | |
['DCBCHGD', 'WRWWWWWRRW'], | |
['JEABDHD', 'RRWWRRRWRR'], | |
['JHFADHE', 'RWWRRRRRWW'], | |
['HDGGDBIB', 'WWWWWWWWWW'], | |
['IIDIHCCG', 'WWWRWRRWWW'], | |
# ['BBFBICIE', 'WRRWRRRWWW'], | |
['HJHCFBJGG', 'WRRWWWWRRW'], | |
['AJJIEAAII', 'RWWWRWWWWR'], | |
['AIDHJFGAE', 'WWWRRWWWWW'], | |
['FGBGHCBHJJ', 'WWRWWWWRRW'], | |
['EFIGIGGHHJ', 'WWWWRRRWWR'], | |
['HGAFDIFFFF', 'RWWRWRRRRW'], | |
['AABBCCDDEE', 'WWWWWWWWWW'], | |
['ABCDEFGHIJ', 'RRRRRWWWWW'], | |
['FGHIJABCDE', 'RRRRRRRRRR'], | |
] | |
expected_result.each do |input, output| | |
flash_star = FlashStar.new | |
input.split('').each do |label| | |
flash_star.emit(label.to_sym) | |
end | |
puts flash_star.to_s == output | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment