khaaan

Solver for 0hh1.com puzzles using clingo

Nov 14th, 2014
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. % USAGE: clingo -n 0 <thisfile.txt # show all solutions to puzzle
  2. %
  3. % Works with clingo 3 (NOT clingo 4). Download from:
  4. % http://sourceforge.net/projects/potassco/files/clingo/3.0.5/
  5. %
  6. % See https://news.ycombinator.com/item?id=8606193
  7.  
  8. % Two colors: 1 = red, 2 = blue. (0 = empty)
  9. color(1). color(2).
  10.  
  11. #const size = 6.
  12. % coord(Y,X) = Y,X is a valid coordinate of a cell in the puzzle.
  13. coord(0..size-1,0..size-1).
  14.  
  15. % p(Y,X,C) = cell [Y,X] is color C in the original puzzle.
  16. % - O - - - -
  17. % X X O O X O
  18. % - - - - - -
  19. % O - X - O -
  20. % - - - - - O
  21. % X - O - X -
  22. p(0,0,0). p(0,1,1). p(0,2,0). p(0,3,0). p(0,4,0). p(0,5,0).
  23. p(1,0,2). p(1,1,2). p(1,2,1). p(1,3,1). p(1,4,2). p(1,5,1).
  24. p(2,0,0). p(2,1,0). p(2,2,0). p(2,3,0). p(2,4,0). p(2,5,0).
  25. p(3,0,1). p(3,1,0). p(3,2,2). p(3,3,0). p(3,4,1). p(3,5,0).
  26. p(4,0,0). p(4,1,0). p(4,2,0). p(4,3,0). p(4,4,0). p(4,5,1).
  27. p(5,0,2). p(5,1,0). p(5,2,1). p(5,3,0). p(5,4,2). p(5,5,0).
  28.  
  29. % q(Y,X,C) = cell[Y,X] is color C in the solved puzzle.
  30.  
  31. % The colors in the original puzzle must not change in the solution.
  32. q(Y,X,C) :- p(Y,X,C), C != 0.
  33.  
  34. % Each empty square in the original puzzle must be colored red or blue in the solution.
  35. 1 { q(Y,X,C) : color(C) } 1 :- p(Y,X,0).
  36.  
  37. % f(Y,X) = cell(Y,X) is filled with some color.
  38. f(Y,X) :- q(Y,X,C), C != 0.
  39.  
  40. % Solution cannot have empty squares.
  41. :- coord(Y,X), not f(Y,X).
  42.  
  43. % Solution cannot have three adjacent cells of the same color horizontally.
  44. :- q(Y,X,C), q(Y,X+1,C), q(Y,X+2,C).
  45.  
  46. % Solution cannot have three adjacent cells of the same color vertically.
  47. :- q(Y,X,C), q(Y+1,X,C), q(Y+2,X,C).
  48.  
  49. % Exactly half of the cells in every row in the solution must be red.
  50. :- K = #count { q(Y,X,1) : coord(_,X) }, coord(Y,_), K != size/2.
  51.  
  52. % Exactly half of the cells in every column in the solution must be red.
  53. :- K = #count { q(Y,X,1) : coord(Y,_) }, coord(_,X), K != size/2.
  54.  
  55. % diffrow(Y1, Y2) = rows Y1 and Y2 differ.
  56. diffrow(Y1, Y2) :- q(Y1,X,C1), q(Y2,X,C2), C1 != C2, coord(Y1,_), coord(Y2,_), Y1 < Y2.
  57.  
  58. % diffcol(X1, X2) = columns X1 and X2 differ.
  59. diffcol(X1, X2) :- q(Y,X1,C1), q(Y,X2,C2), C1 != C2, coord(_,X1), coord(_,X2), X1 < X2.
  60.  
  61. % Solution cannot have duplicate rows.
  62. :- coord(Y1,_), coord(Y2,_), Y1 < Y2, not diffrow(Y1, Y2).
  63.  
  64. % Solution cannot have duplicate columns.
  65. :- coord(_,X1), coord(_,X2), X1 < X2, not diffcol(X1, X2).
  66.  
  67. #hide.
  68. #show q/3. % Show the solution
Advertisement
Add Comment
Please, Sign In to add comment