Skip to content

Instantly share code, notes, and snippets.

@okitan
Created October 14, 2012 03:22
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 okitan/3887129 to your computer and use it in GitHub Desktop.
Save okitan/3887129 to your computer and use it in GitHub Desktop.
class Tetoroid
def self.recognize(inputs)
return "-" if inputs.uniq.size != 4
coordinates = inputs.map {|n| n.divmod(10) }
# g means gravity
gx, gy = coordinates.transpose.map {|array| array.inject(&:+).to_f / array.size }
distances = coordinates.map {|x, y| (x - gx)**2 + (y - gy)**2 }.sort # dare not sqrt
{ [ 0.25, 0.25, 2.25, 2.25 ] => "I",
[ 0.125, 0.625, 1.125, 1.625 ] => "L",
[ 0.0625, 0.5625, 1.0625, 1.0625 ] => "T",
[ 0.5, 0.5, 0.5, 0.5 ] => "O",
[ 0.25, 0.25, 1.25, 1.25 ] => "S",
}[distances] || "-"
end
end
describe Tetoroid do
subject { described_class }
L = "L"
I = "I"
T = "T"
O = "O"
S = "S"
[ [ [ 55, 55, 55, 55 ] ],
[ [ 07, 17, 06, 05 ], L ],
[ [ 21, 41, 31, 40 ], L ],
[ [ 62, 74, 73, 72 ], L ],
[ [ 84, 94, 74, 75 ], L ],
[ [ 48, 49, 57, 47 ], L ],
[ [ 69, 89, 79, 68 ], L ],
[ [ 90, 82, 91, 92 ], L ],
[ [ 13, 23, 03, 24 ], L ],
[ [ 24, 22, 25, 23 ], I ],
[ [ 51, 41, 21, 31 ], I ],
[ [ 64, 63, 62, 65 ], I ],
[ [ 49, 69, 59, 79 ], I ],
[ [ 12, 10, 21, 11 ], T ],
[ [ 89, 99, 79, 88 ], T ],
[ [ 32, 41, 43, 42 ], T ],
[ [ 27, 16, 36, 26 ], T ],
[ [ 68, 57, 58, 67 ], O ],
[ [ 72, 62, 61, 71 ], O ],
[ [ 25, 24, 15, 14 ], O ],
[ [ 43, 54, 53, 42 ], S ],
[ [ 95, 86, 76, 85 ], S ],
[ [ 72, 73, 84, 83 ], S ],
[ [ 42, 33, 32, 23 ], S ],
[ [ 66, 57, 67, 58 ], S ],
[ [ 63, 73, 52, 62 ], S ],
[ [ 76, 68, 77, 67 ], S ],
[ [ 12, 11, 22, 01 ], S ],
[ [ 05, 26, 06, 25 ] ],
[ [ 03, 11, 13, 01 ] ],
[ [ 11, 20, 00, 21 ] ],
[ [ 84, 95, 94, 86 ] ],
[ [ 36, 56, 45, 35 ] ],
[ [ 41, 33, 32, 43 ] ],
[ [ 75, 94, 84, 95 ] ],
[ [ 27, 39, 28, 37 ] ],
[ [ 45, 34, 54, 35 ] ],
[ [ 24, 36, 35, 26 ] ],
[ [ 27, 27, 27, 27 ] ],
[ [ 55, 44, 44, 45 ] ],
[ [ 70, 73, 71, 71 ] ],
[ [ 67, 37, 47, 47 ] ],
[ [ 43, 45, 41, 42 ] ],
[ [ 87, 57, 97, 67 ] ],
[ [ 49, 45, 46, 48 ] ],
[ [ 63, 63, 52, 72 ] ],
[ [ 84, 86, 84, 95 ] ],
[ [ 61, 60, 62, 73 ] ],
[ [ 59, 79, 69, 48 ] ],
[ [ 55, 57, 77, 75 ] ],
].each do |inputs, output = "-"|
it { subject.recognize(inputs).should == output }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment