Created
February 21, 2011 00:29
-
-
Save rnaud/836468 to your computer and use it in GitHub Desktop.
I'm only finding a 6 card deck
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 'json' | |
class Array | |
def rotate n | |
a =dup | |
n.times do a << a.shift end | |
a | |
end | |
end | |
@grid = [[{"type"=>"p", "head" => 1},{"type"=>"c", "head" => 1},{"type"=>"a", "head" => 2},{"type"=>"o", "head" => 2}], | |
[{"type"=>"o", "head" => 1},{"type"=>"a", "head" => 2},{"type"=>"c", "head" => 2},{"type"=>"p", "head" => 1}], | |
[{"type"=>"c", "head" => 1},{"type"=>"p", "head" => 2},{"type"=>"o", "head" => 2},{"type"=>"a", "head" => 1}], | |
[{"type"=>"p", "head" => 1},{"type"=>"c", "head" => 2},{"type"=>"o", "head" => 2},{"type"=>"a", "head" => 1}], | |
[{"type"=>"p", "head" => 2},{"type"=>"c", "head" => 2},{"type"=>"a", "head" => 1},{"type"=>"c", "head" => 1}], | |
[{"type"=>"a", "head" => 1},{"type"=>"p", "head" => 2},{"type"=>"o", "head" => 2},{"type"=>"p", "head" => 1}], | |
[{"type"=>"a", "head" => 1},{"type"=>"o", "head" => 1},{"type"=>"a", "head" => 2},{"type"=>"c", "head" => 2}], | |
[{"type"=>"o", "head" => 1},{"type"=>"a", "head" => 2},{"type"=>"c", "head" => 2},{"type"=>"p", "head" => 1}], | |
[{"type"=>"p", "head" => 1},{"type"=>"c", "head" => 2},{"type"=>"o", "head" => 2},{"type"=>"a", "head" => 1}]] | |
@new_grid = [nil, nil, nil,nil, nil, nil,nil, nil, nil] | |
@used = [false, false, false,false, false, false,false, false, false] | |
def check_validity(card, position, orientation) | |
# since I'm adding from top left to bottom, I only need to check top and left | |
try_card = @grid[card].rotate orientation | |
valid = true | |
# top | |
if (@new_grid[position-3]) | |
if (try_card[0]["type"] != @new_grid[position-3][2]["type"] || try_card[0]["head"] == @new_grid[position-3][2]["head"]) | |
valid = false | |
end | |
end | |
# left | |
if (@new_grid[position-1] && (position % 3) != 0) | |
if (try_card[3]["type"] != @new_grid[position-1][1]["type"] || try_card[3]["head"] == @new_grid[position-1][1]["head"]) | |
valid = false | |
end | |
end | |
return valid | |
end | |
def solve_puzzle(position) | |
(0..8).each do |card| | |
unless (@used[card]) | |
(0..3).each do |orientation| | |
if (check_validity(card, position, orientation)) | |
@used[card] = true | |
@new_grid[position] = @grid[card].rotate orientation | |
if position == 7 | |
puts @new_grid.to_json | |
end | |
if (position < 8) | |
solve_puzzle(position + 1) | |
else | |
puts "I WON" | |
puts @new_grid.to_json | |
end | |
@new_grid[position] = nil | |
@used[card] = false | |
end | |
end | |
end | |
end | |
end | |
solve_puzzle(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment