Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. ## solve stauf painting puzzle
  2. get.move = function(val){
  3. move = c(0,0,0,0,0,0,0,0,0)
  4. if(val == 1) move = c(1,1,0,1,1,0,0,0,0)
  5. if(val == 2) move = c(1,1,1,0,0,0,0,0,0)
  6. if(val == 3) move = c(0,1,1,0,1,1,0,0,0)
  7. if(val == 4) move = c(1,0,0,1,0,0,1,0,0)
  8. if(val == 5) move = c(0,1,0,1,1,1,0,1,0)
  9. if(val == 6) move = c(0,0,1,0,0,1,0,0,1)
  10. if(val == 7) move = c(0,0,0,1,1,0,1,1,0)
  11. if(val == 8) move = c(0,0,0,0,0,0,1,1,1)
  12. if(val == 9) move = c(0,0,0,0,1,1,0,1,1)
  13. return(move)
  14. }
  15.  
  16. compose.vectors = function(v1,v2){
  17. return((v1+v2) %% 3)
  18. }
  19.  
  20. compose = function(puz,vals){
  21. for(val in vals){ puz = compose.vectors(puz,get.move(val)) }
  22. return(puz)
  23. }
  24.  
  25. solve.puz = function(init){
  26. puzzle = init
  27. moves = c()
  28. while(!all(puzzle == rep(0,9))){
  29. new.move = sample(1:9,1)
  30. moves = c(moves,new.move)
  31. moves = remove.dupes(moves)
  32. puzzle = compose(puzzle,new.move)
  33. }
  34. return(moves)
  35. }
  36.  
  37. remove.dupes = function(moves){
  38. t = table(moves)
  39. if(any(t > 2)){
  40. moves.to.delete = names(which(t > 2))
  41. ind = moves %in% moves.to.delete
  42. moves = moves[!ind]
  43. }
  44. return(moves)
  45. }
  46.  
  47. ##
  48.  
  49. init = c(0,1,1,0,0,1,0,0,0)
  50. init = c(2,2,0,2,2,0,0,0,0)
  51. solution = solve.puz(init)
  52. print(sort(solution))
  53.  
  54. # can use compose to specify moves
  55. compose(init,1)
  56. compose(init,c(1,1,3,4,2,8))
  57.  
  58. ############
  59.  
  60. ## Much faster solution
  61. move.one.pos = function(position){
  62. ret.moves = rep(0,9)
  63. if(position == 1) ret.moves = c(1,3,5,6,6,7,8,8,9,9)
  64. if(position == 2) ret.moves = c(1,2,3,4,5,6,8,8)
  65. if(position == 3) ret.moves = c(1,3,4,4,5,7,7,8,8,9)
  66. if(position == 4) ret.moves = c(1,2,4,5,6,6,7,8)
  67. if(position == 5) ret.moves = c(1,2,3,4,6,7,8,9)
  68. if(position == 6) ret.moves = c(2,3,4,4,5,6,8,9)
  69. if(position == 7) ret.moves = c(1,2,2,3,3,5,6,6,7,9)
  70. if(position == 8) ret.moves = c(2,2,4,5,6,7,8,9)
  71. if(position == 9) ret.moves = c(1,1,2,2,3,4,4,5,7,9)
  72. return(ret.moves)
  73. }
  74.  
  75. remove.all.dupes = function(prev.moves){
  76. moves = c()
  77. for(i in 1:9){
  78. if(!(i %in% prev.moves)) next
  79. n_vals = length(which(prev.moves == i))
  80. n.rep = n_vals %% 3
  81. if(n.rep == 0) next
  82. moves = c(moves,rep(i,n.rep))
  83. }
  84. return(moves)
  85. }
  86.  
  87. solve.puz.2 = function(puz){
  88. moves = c()
  89. for(i in seq_along(puz)){
  90. how.many.to.press = (3 - puz[i]) %% 3
  91. if(how.many.to.press > 0){
  92. for(j in 1:how.many.to.press) moves = c(moves,move.one.pos(i))
  93. }
  94. }
  95. moves = sort(remove.all.dupes(moves))
  96. return(moves)
  97. }
  98.  
  99. init = c(2,1,0,2,2,1,0,1,0)
  100. solve.puz.2(init)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement