Created
October 15, 2015 01:40
-
-
Save lxfontes/b632a649fab6ba178c3e to your computer and use it in GitHub Desktop.
Generate deck of cards with single link
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
| #!/usr/bin/env ruby | |
| # use prime numbers | |
| def generate_deck(size = 7) | |
| cards = [] | |
| (0...size).each do |i| | |
| (0...size).each do |j| | |
| card = [] | |
| (0...size).each do |k| | |
| h = ((i*k+j) % size) * size + k | |
| card << h | |
| end | |
| h = size * size + i | |
| card << h | |
| cards << card | |
| end | |
| end | |
| (0...size).each do |i| | |
| card = [] | |
| (0...size).each do |j| | |
| h = j * size + i | |
| card << h | |
| end | |
| h = size * size + size | |
| card << h | |
| cards << card | |
| end | |
| card = [] | |
| (0..size).each do |i| | |
| h = size * size + i | |
| card << h | |
| end | |
| cards << card | |
| cards | |
| end | |
| def validate(check_card, cards) | |
| cards.each do |card| | |
| next if check_card == card | |
| matches = 0 | |
| check_card.each do |n| | |
| matches += 1 if card.include?(n) | |
| end | |
| raise("Card #{check_card.inspect} and #{card.inspect} with #{matches} matches") if matches != 1 | |
| end | |
| end | |
| cards = generate_deck(7) | |
| cards.each do |card| | |
| validate(card, cards) | |
| end | |
| cards.each do |card| | |
| puts(card.join(' ')) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment