Created
March 3, 2018 15:49
-
-
Save Cxarli/458603df7eb0cf3913ad0cc081e3a586 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
import sys | |
from math import sqrt | |
errors = 0 | |
def is_uniq(lst): | |
return len(list(set(lst))) is len(lst) | |
def flatten(lst): | |
res = [] | |
for item in lst: | |
if isinstance(item, list): | |
res.extend(flatten(item)) | |
else: | |
res.append(item) | |
return res | |
raw_sudoku = [] | |
if len(sys.argv) <= 1: | |
print("Not enough arguments") | |
exit(1) | |
filename = sys.argv[1] | |
with open(filename) as f: | |
for line in f: | |
raw_sudoku.append([int(x) for x in line.split(" ")]) | |
# Check dimensions | |
height = len(raw_sudoku) | |
width = len(raw_sudoku[0]) | |
if width is not height: | |
print("Width and height not the same") | |
errors += 1 | |
for i in range(0, height): | |
row = raw_sudoku[i] | |
if len(row) is not width: | |
print("Row " + str(i+1) + " is not long enough") | |
errors += 1 | |
# Check rows | |
for i in range(0, len(raw_sudoku[0])): | |
row = raw_sudoku[i] | |
if not is_uniq(row): | |
print("Doubles on line " + str(i+1)) | |
errors += 1 | |
# Check columns | |
for i in range(0, len(raw_sudoku)): | |
items = [row[i] for row in raw_sudoku] | |
if not is_uniq(items): | |
print("Doubles on columns " + str(i+1)) | |
errors += 1 | |
# Create sections | |
sec_width = int(sqrt(len(raw_sudoku))) | |
sections = [] | |
for y in range(0, sec_width): | |
for x in range(0, sec_width): | |
# ~ Magic ~ | |
section = [ | |
row[x*sec_width:(x+1) * sec_width] | |
for row in raw_sudoku[y*sec_width:(y+1) * sec_width] | |
] | |
sections.append(section) | |
# Check sections | |
for i in range(0, sec_width): | |
section = sections[i] | |
if not is_uniq(flatten(section)): | |
print("Section " + str(i + 1) + " is not unique") | |
errors += 1 | |
if errors is 0: | |
print("No errors :)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment