/* Pi Day Sudoku (2009) in Picat. "Pi Day 2009" http://brainfreezepuzzles.com/main/piday2009.html Via 360 "Pi Day Sudoku 2009" http://threesixty360.wordpress.com/2009/03/09/pi-day-sudoku-2009/ """ Each row, column, and region contains the digits 1-9 exactly once plus three π symbols. There’s a printable .pdf file here [ http://brainfreezepuzzles.com/main/files/Brainfreeze_PiDay2009.pdf ] As a bonus, if you send a correct solution in to Brainfreeze puzzles in the next couple months, you're eligible for a drawing for their book on Color Sudoku! More details are on their website [[http://brainfreezepuzzles.com/]]. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. import cp. main => go. go => N = 12, data(X), regions(Regions), time2(All = findall(X, sudoku_pi(N, Regions, X))), foreach(XX in All) print_sudoku(XX) end, nl. print_sudoku(X) => Pi = 10, N = X.length, foreach(I in 1..N) foreach(J in 1..N) if X[I,J] = Pi then print("P") else print(X[I,J]) end, print(" ") end, nl end, nl. sudoku_pi(N, Regions, X) => % Occurrences for 1..9 and Pi % 1 2 3 4 5 6 7 8 9 Pi Occ = [1, 1, 1, 1, 1, 1, 1, 1, 1, 3], OccD = [$I-Occ[I] : I in 1..10], % for global_cardinality/2 NumRegions = Regions.length, % decision variables X.flatten() :: 1..10, % constraints % rows foreach(I in 1..N) check([X[I,J] : J in 1..N],OccD) end, % columns foreach(J in 1..N) check([X[I,J] : I in 1..N],OccD) end, % the regions foreach(I in 1..NumRegions) Region = Regions[I], check([X[Region[J,1],Region[J,2]] : J in 1..N], OccD) end, solve($[ff,split],X). check(X, Occ) => global_cardinality(X, Occ). % using count/4 check2(X, Occ) => foreach($K-D in Occ) count(K,X,#=,D) end. data(Data) => P = 10, % Pi Data = [ [4,9,7, P,5,_,_,_,_, _,_,_], [_,P,_, 8,_,_,9,6,1, 5,2,_], [_,8,_, 1,_,_,_,P,_, 7,_,_], [_,_,_, _,_,_,_,P,_, 4,_,_], [5,3,9, 6,_,_,_,_,_, _,_,_], [9,4,_, P,P,P,7,_,_, _,_,_], [_,_,_, _,_,6,2,5,P, _,7,4], [_,_,_, _,_,_,_,_,P, P,3,8], [_,7,8, 4,6,9,_,_,_, _,_,_], [_,_,3, _,P,_,_,4,7, 1,6,9], [_,_,4, _,1,_,_,_,6, _,P,_], [_,_,_, _,_,_,_,_,4, _,5,_] ]. % % The regions % regions(Regions) => Regions = [ % Upper left dark green [[1,1] , [1,2] , [1,3] , [2,1] , [2,2] , [2,3] , [3,1] , [3,2] , [4,1] , [4,2] , [5,1] , [5,2]] , % Upper mid light dark green [[1,4] , [1,5] , [1,6] , [1,7] , [1,8] , [1,9] , [2,4] , [2,5] , [2,6] , [2,7] , [2,8] , [2,9] ], % Upper right green [[1,10] , [1,11] , [1,12] , [2,10] , [2,11] , [2,12] , [3,11] , [3,12] , [4,11] , [4,12] , [5,11] , [5,12]] , % Mid upper left "blue" [[3,3] , [3,4] , [3,5] , [3,6] , [4,3] , [4,4] , [4,5] , [4,6] , [5,3] , [5,4] , [5,5] , [5,6]] , % Mid Upper right blue [[3,7] , [3,8] , [3,9] , [3,10] , [4,7] , [4,8] , [4,9] , [4,10] , [5,7] , [5,8] , [5,9] , [5,10]] , % Mid left green [[6,1] , [6,2] , [6,3] , [7,1] , [7,2] , [7,3] , [8,1] , [8,2] , [8,3] , [9,1] , [9,2] , [9,3]] , % Mid left blue [[6,4] , [6,5] , [7,4] , [7,5] , [8,4] , [8,5] , [9,4] , [9,5] , [10,4] , [10,5] , [11,4] , [11,5]] , % Mid mid green [[6,6] , [6,7] , [7,6] , [7,7] , [8,6] , [8,7] , [9,6] , [9,7] , [10,6] , [10,7] , [11,6] , [11,7]] , % Mid right blue [[6,8] , [6,9] , [7,8] , [7,9] , [8,8] , [8,9] , [9,8] , [9,9] , [10,8] , [10,9] , [11,8] , [11,9]] , % Mid right green [[6,10] , [6,11] , [6,12] , [7,10] , [7,11] , [7,12] , [8,10] , [8,11] , [8,12] , [9,10] , [9,11] , [9,12]] , % Lower left dark green [[10,1] , [10,2] , [10,3] , [11,1] , [11,2] , [11,3] , [12,1] , [12,2] , [12,3] , [12,4] , [12,5] , [12,6]] , % Lower right dark green [[10,10] , [10,11] , [10,12] , [11,10] , [11,11] , [11,12] , [12, 7] , [12, 8] , [12, 9] , [12,10] , [12,11] , [12,12]] ].