diff options
author | Eric Anderson <ejona86@gmail.com> | 2014-06-30 23:06:05 -0700 |
---|---|---|
committer | Eric Anderson <ejona86@gmail.com> | 2014-06-30 23:25:55 -0700 |
commit | d4185a5d6b89ef179c6e3ebe94e072f8a180a68e (patch) | |
tree | 0bf4ac7ed22bc9bb19205627ffefd9c4f4ae78c0 /capset.py | |
parent | 022843b76031d925fc5f93ab088512bfad5ad65b (diff) | |
download | capset-d4185a5d6b89ef179c6e3ebe94e072f8a180a68e.tar.gz capset-d4185a5d6b89ef179c6e3ebe94e072f8a180a68e.zip |
Faithful port to Go
It is around 60 times faster
Diffstat (limited to 'capset.py')
-rwxr-xr-x | capset.py | 70 |
1 files changed, 0 insertions, 70 deletions
diff --git a/capset.py b/capset.py deleted file mode 100755 index 65341b9..0000000 --- a/capset.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/env python3 -# A capset is a collection of cards that contains no sets. -# The supposed maximal cardinality of a capset in Set is 20. -# I wish to find such a collection with this program. - -from random import shuffle -from itertools import combinations - -def is_set(cards): - if len(cards) != 3: - raise Exception("Not the right number of cards.") - for i in range(4): - if cards[0][i] == cards[1][i] == cards[2][i]: continue - if cards[0][i] != cards[1][i] != cards[2][i] != cards[0][i]: continue - return False - return True - -def int2card(num): - if num != int(num): - raise Exception("Number must be an integer.") - if num < 0: - raise Exception("Number must be nonnegative.") - if num > 80: - raise Exception("Number must be less than 81.") - strlist = [] - for i in range(4): - strlist.append(str(num%3)) - num //= 3 - return ''.join(strlist[::-1]) - -def increment_tableau(tableau, deck, pop=False): - index = deck.index(tableau[-1]) + 1 - if index != len(deck): - if pop: tableau.pop() - tableau.append(deck[index]) - return - tableau.pop() - index = deck.index(tableau.pop()) + 1 - if len(tableau) == 1: - print("incrementing second card to index", index) - if index + 18 > len(deck): quit() - tableau.append(deck[index]) - -def check_tableau(tableau): - if len(tableau) == 2: return True - for comb in combinations(tableau, 3): - if is_set(comb): return False - return True - -def main(): - outfile = open("./capset.out", mode="w") - tableau = ["2222"] - deck = reversed(range(80)) # all cards other than 2222 - deck = list(map(int2card, deck)) - #shuffle(deck) - tableau.append(deck[0]) - maxim = 20 - while True: - if check_tableau(tableau): - if len(tableau) >= maxim: - print(tableau) - print(tableau, file=outfile, flush=True) - maxim = len(tableau) - increment_tableau(tableau, deck) - else: - increment_tableau(tableau, deck, pop=True) - outfile.close() - -if __name__ == "__main__": main() - |