440 likes | 1.19k Views
Solving Kenken Puzzles – By Not Playing. John R. Gerlach PhilaSUG Meeting @ RPS June 8, 2010. Kenken Puzzles. Latin Square Integers (Row / Column Unique) Partitioned into Cages. Cages Contiguous Cells Arithmetic Operator Computed Total. Kenken Puzzle – Example.
E N D
Solving Kenken Puzzles –By Not Playing John R. Gerlach PhilaSUG Meeting @ RPS June 8, 2010
Kenken Puzzles • Latin Square • Integers (Row / Column Unique) • Partitioned into Cages. • Cages • Contiguous Cells • Arithmetic Operator • Computed Total
Assumptions • # Cells Per Cage L.E. Dimension • 4x4 Puzzle --> # Cells: 1-4 • 6x6 Puzzle --> # Cells: 1-6 • Puzzle is Well-Constructed • Unique Solution
Define Viable Sets • An ordered collection of Natural numbers ranging from 1 to N whose values may be used more than once, however, cannot be consecutive. • Valid {1,2,3} {3,2,3} • Invalid {1,1,2} {1,2,3,3} {1,null,2}
Define Viable Sets • Addition & Multiplication • Sets {n1,n2}; {n1,n2,n3}; {n1,n2,n3,n4} • Subtraction • Sets {2,6} and {6,2} = 4, not -4 • Division • Sets {2,6} and {6,2} = 3, not 0.33 • Sets {1,2} and {2,1} = 2, not 0.50
Define Viable Sets • Determined by Cage • Contiguous Cells • Arithmetic Operation • Total
Arithmetic Operations • Addition All Numbers • Multiplication All Numbers • Subtraction Two Numbers • Division Two Numbers
Singular Sets • Cage represents one cell having a number assigned at the onset of the puzzle. {1}, {2}, {3}, {4}, …, {n}
Generate Viable Sets data p4sets; do n1=1,2,3,4; do n2=1,2,3,4; do n3=.,1,2,3,4; do n4=.,1,2,3,4; < Determine Viability of Set > end; end; end; end; run;
Generate Viable Sets • 4x4 Puzzle 156 • 5x5 Puzzle 1,700 • 6x6 Puzzle 23,430 • 8x8 Puzzle 7,686,392 • 9x9 Puzzle 172,565,640
Generate Viable Sets Σd (d-1)n-1 Where d - Dimension of puzzle n - Number of Cells Σ - Summation, n = 2 to d.
Generate Viable Sets 6x6 Puzzle [ 6(6-1)2-1 ] + [ 6(6-1)3-1 ] + [ 6(6-1)4-1 ] + [ 6(6-1)5-1 ] + [ 6(6-1)6-1 ] = 30 + 150 + 750 + 3,750 + 18,750 = 23,430 Viable Sets
Generate Viable Sets ---- Numbers ----- ------- Operators -------- Set 1 2 3 4 + - x / --------------------------------------------------- 1 1 2 . . 3 1 2 2 2 1 2 1 . 4 . 2 . 3 1 2 1 2 6 . 4 . 4 1 2 1 3 7 . 6 . : : : 9 1 2 3 4 10 . 24 . 10 1 2 4 . 7 . 8 . 11 1 2 4 1 8 . 8 . 12 1 2 4 2 9 . 16 . 13 1 2 4 3 10 . 24 . 14 1 3 . . 4 2 3 3
Generate Viable Sets ======== Numbers ========= 1 2 3 4 ADD SUBT MULT DIV ----------------------------------------------------------- ^Miss ^Miss Miss Miss 12 12 12 8 ^Miss ^Miss ^Miss Miss 36 . 36 . ^Miss ^Miss ^Miss ^Miss 108 . 108 . ===== ===== ===== ===== 156 12 156 8
Determine Viability • First two numbers are equal. • {1,1,null,null} • 2nd and 3rd numbers are equal and not null. • {1,2,2,null} • 3rd and 4th numbers are equal and not null. • {1,3,1,1} • 3rd is null and 4th is not null. • {1,3,null,2}
Compute TotalsAddition & Multiplication • add = sum(of n1-n4); • if n2 eq . then mult = n1; else if n3 eq . then mult = n1 * n2; else if n4 eq . then mult = n1 * n2 * n3; else mult = n1 * n2 * n3 * n4;
Compute TotalsSubtraction & Division if n3 eq . and n4 eq . then do; if n1 gt n2 then subt = n1 – n2; else subt = n2 – n1; if n1 gt 2 and int(n1/n2) eq n1/n2 then div = n1 / n2; else if n2 gt n1 and int(n2/n1) eq n2/n1 then div = n2 / n1; end; else do; subt = .; div = .; end;
Define a Puzzle • What does the data file look like? • How do you render the puzzle?
Define 4x4 Puzzles 1 C1 3 + 1,1 1,2 1 C2 7 + 1,3 1,4 1 C3 5 + 2,1 2,2 1 C4 5 + 2,3 2,4 1 C5 7 + 3,1 3,2 1 C6 3 + 3,3 3,4 1 C7 5 + 4,1 4,2 1 C8 5 + 4,3 4,4 2 C1 10 + 1,1 1,2 1,3 2,3 2 C2 10 + 1,4 2,4 3,4 4,4 2 C3 9 + 2,1 3,1 4,1 2 C4 8 + 2,2 3,2 4,2 2 C5 3 + 3,3 4,3
Define 6x6 Puzzle 1 C1 3 . 1,1 1 C2 6 + 1,2 2,2 1 C3 11 + 1,3 2,3 1 C4 11 + 1,4 1,5 2,5 1 C5 3 . 2,4 1 C6 6 + 1,6 2,6 1 C7 6 + 2,1 3,1 1 C8 3 . 3,2 1 C9 6 + 3,3 4,3 5,3 1 C10 1 - 3,4 4,4 1 C11 12 + 3,5 3,6 4,6 1 C12 3 . 4,5 1 C13 6 . 4,1 1 C14 6 + 4,2 5,2 1 C15 13 + 5,4 5,5 6,5 1 C16 4 + 5,6 6,6 1 C17 4 - 5,1 6,1 1 C18 10 + 6,2 6,3 1 C19 2 . 6,4
Render 6x6 Puzzle ------------------------------- Column --------------------------- Row 1 2 3 4 5 6 -------------------------------------------------------------------------- 1 C1[ 3 ] C2[ 6 +] C3[ 11 +] C4[ 11 +] C4[ 11 +] C6[ 6 +] 2 C7[ 6 +] C2[ 6 +] C3[ 11 +] C5[ 3 ] C4[ 11 +] C6[ 6 +] 3 C7[ 6 +] C8[ 3 ] C9[ 6 +] C10[ 1 -] C11[ 12 +] C11[ 12 +] 4 C13[ 6 ] C14[ 6 +] C9[ 6 +] C10[ 1 -] C12[ 3 ] C11[ 12 +] 5 C17[ 4 -] C14[ 6 +] C9[ 6 +] C15[ 13 +] C15[ 13 +] C16[ 4 +] 6 C17[ 4 -] C18[ 10 +] C18[ 10 +] C19[ 2 ] C15[ 13 +] C16[ 4 +]
Solve Puzzle • Using Conventional Method • By Not Playing (i.e. Cheating)
Solve Puzzle By Conventional Method Too Much Trouble !! Rather Play Piano, Watch Italian TV, or Drink Heineken.
Solve Puzzle By Not Playing • Obtain Viable Sets for Each Cage • Perform Cartesian Product • Check Possible Solutions • Print Solution
Obtain Viable Sets Cage 1: 3 Cells, 6+ Cage 2: 2 Cells, 2- 1 2 3 . 1 3 1 3 2 . 2 4 1 4 1 . 3 1 2 1 3 . 4 2 2 3 1 . 3 1 2 . 3 2 1 .
Obtain Viable Sets Cage 3: 4 Cells, 4* Cage 4: 2 Cells, 3/ 1 2 1 2 1 3 2 1 2 1 3 1
Perform Cartesian Product • Combine every set representing Cage 1, with every set representing Cage 2, with every set representing Cage 3, : : : with every set representing Cage n-1, with every set representing Cage n.
Check Possible Solution • Put collection of Viable Sets in grid and check for Row / Column uniqueness. • Involved SAS Code. • Macro Variables, CAGES, VSETS • 2-Dimensional Matrices (Cage, Cell) • CAGES • VSETS • Puzzle
Print Solution data rep; array puzzle{4,4} r1c1-r1c4 r2c1-r2c4 r3c1-r3c4 r4c1-r4c4; array cols{4} col1-col4; set kenken end=eof; do row = 1 to 4; do col = 1 to 4; cols{col} = puzzle{row,col}; end; output; end; keep row col1-col4; run;
Print Solution proc report data=rep nowindows headskip split='!'; columns row col1-col4; define row / order noprint; define col1 / display format=5. center '00'x; define col2 / display format=5. center '00'x; define col3 / display format=5. center '00'x; define col4 / display format=5. center '00'x; break after row / skip; title1 'KenKen 4x4 Puzzle -- Solution'; run;
SAS Solution • Works in Theory. • Even Works for 4x4 Puzzles. • Fails in Practice! • Reason --> Cartesian Product.
18 Cages 4 Singular Cages 10 Two-cell Cages 4 Three-cell Cages All Arithmetic Ops 24,883,200 Aggregate Sets C1_VSETS 1 C2_VSETS 1 C3_VSETS 2 C4_VSETS 6 C5_VSETS 2 C6_VSETS 10 C7_VSETS 1 C8_VSETS 1 C9_VSETS 6 C10_VSETS 2 C11_VSETS 2 C12_VSETS 6 C13_VSETS 1 C14_VSETS 6 C15_VSETS 2 C16_VSETS 15 C17_VSETS 1 C18_VSETS 4 Why Solution “Fails”
17 Cages 3 Singular Cages 10 Two-cell Cages 3 Three-cell Cages 1 Four-cell Cage All Arithmetic Ops 1,184,440,320,000 Aggregate Sets C1_VSETS 4 C2_VSETS 10 C3_VSETS 6 C4_VSETS 4 C5_VSETS 2 C6_VSETS 1 C7_VSETS 15 C8_VSETS 6 C9_VSETS 1 C10_VSETS 10 C11_VSETS 10 C12_VSETS 6 C13_VSETS 7 C14_VSETS 102 C15_VSETS 1 C16_VSETS 4 C17_VSETS 2 Why Solution “Fails”
Lessons Learned • Viable Sets. • Cardinality of Viable Sets. • Computing Totals. • Limitation of Cartesian Product. • Alternative Solution. • Revised Perception of the Problem.
Thanks • Christine Clarke, PhD Executive Director, Biostatistics & Programming ReSearch Pharmaceutical Services, Inc.
Questions • John R. Gerlach SAS / CDISC Specialist JRGerlach@optonline.net