Add puzzles and puzzles.py from the old branch

This commit is contained in:
Eryn Wells 2017-10-08 08:26:18 -07:00
parent 5696c087d8
commit eae05487b9
3 changed files with 179 additions and 0 deletions

34
puzzles.py Normal file
View file

@ -0,0 +1,34 @@
#!env python3
# Eryn Wells <eryn@erynwells.me>
'''
Parser for puzzles in the ./puzzles directory.
'''
import sudoku
euler = []
norvig = []
def parse_puzzle_files():
global euler, norvig
print('Parsing Euler puzzles')
euler = list(_get_puzzles('puzzles/euler.txt'))
print('Parsing Norvig puzzles')
norvig = list(_get_puzzles('puzzles/norvig.txt'))
def _get_puzzles(filename):
with open(filename, 'r') as f:
puzzles = f.readlines()
return (_parse_puzzle(p) for p in puzzles if p)
def _parse_puzzle(puzzle):
puzzle = puzzle.strip()
if len(puzzle) == 81:
print("Parsing '{}'".format(puzzle))
board = (int('0' if x == '.' else x) for x in puzzle)
return sudoku.Sudoku(initial=board)
else:
print("Skipping '{}'".format(puzzle))
return None