-
Notifications
You must be signed in to change notification settings - Fork 86
/
solve_sudoku.sh
executable file
·46 lines (35 loc) · 1.07 KB
/
solve_sudoku.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
if [ "$#" -ne 1 ]; then
file=sudokuExample.lisp
else
file=$1
fi
echo -e "Finding solution for:\n"
cat $file
echo -e "\n"
read -n 1 -s -r -p "Press any key to continue"
echo -e "\n"
echo -e "\nEncoding the problem\n"
cat $file | ../translation/compilation/sudoku_encoder > sudokuInput.txt
echo -e "Solving the problem\n"
if ! [ -x "$(command -v lingeling)" ]; then
echo 'Error: lingeling SAT solver is not installed.' >&2
exit 1
fi
cat sudokuInput.txt | lingeling > sudokuOutput.txt
if cat sudokuOutput.txt | grep -q 'UNSATISFIABLE'; then
echo -e "The problem is UNSATISFIABLE\n"
exit
fi
echo "The problem is SATISFIABLE"
echo -e "\n"
read -n 1 -s -r -p "Press any key to continue"
echo -e "\n"
echo -e "Encoding the solution\n"
echo "(" > temp.lisp
cat $file >> temp.lisp
echo "(sat" >> temp.lisp
cat sudokuOutput.txt | grep '^v' | sed 's/v//' | sed 's/ 0//' | sed 's/^ //' | sed 's/ /\n/g' | sed 's/-/\(not /' | sed -e '/not [1-9]*/ s/$/\)/' >> temp.lisp
echo "))" >> temp.lisp
cat temp.lisp | ../translation/compilation/sudoku_encoder
echo -e "\n"