MAKN

panelpuzzle.lua

Dec 2nd, 2021
752
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ppanel = {}
  2. --ppanel = {"0000","0000","0000","0000"}
  3. spanel = {}
  4. --spanel = {"1111","1111","0000","0000"}
  5. --ppanel = {"0000","0000","0000","0000"}
  6.  
  7. function show(p)
  8. for i=1,height,1 do
  9. print(p[i])
  10. end
  11. end
  12.  
  13. function touch(puzzle,x,y)
  14.     panel = {}
  15.     for i=1,height,1 do
  16.     panel[i] = ""
  17.     end
  18.     for i=1,height,1 do
  19.         for j=1,width,1 do
  20.             chr = string.char(string.byte(puzzle[i],j))
  21.             if (i >= y-1 and i <= y+1 and j >= x-1 and j <= x+1) then
  22.                 if (chr == '0') then chr = '1'
  23.                 elseif (chr == '1') then chr = '0'
  24.                 end
  25.             end
  26.             panel[i] = panel[i]..chr
  27.         end
  28.     end
  29.     return panel
  30. end
  31.  
  32. function panelcmp(p1,p2)
  33.     for i=1,width,1 do
  34.         for j=1,height,1 do
  35.             chr1 = string.char(string.byte(p1[j],i))
  36.             chr2 = string.char(string.byte(p2[j],i))
  37.             if (chr1 ~= chr2) then return false end
  38.         end
  39.     end
  40. return true
  41. end
  42. panels = 0
  43. function bruteforce(ps,pp)
  44.     --print(panels)
  45.     if (panelcmp(ps,pp) == true) then print("SOLVED\n");for k=1,height,1 do print(pp[k]) end return true; end
  46.     if (panels < turns) then
  47.         for i=1,height,1 do
  48.             for j=1,width,1 do
  49.                 panels = panels + 1
  50.                 ret = bruteforce(ps,touch(pp,j,i))
  51.                 panels = panels - 1
  52.                 if (ret == true) then print(j,i); for k=1,height,1 do print(pp[k]) end return true; end
  53.             end
  54.         end
  55.     end
  56.     return false;
  57. end
  58. io.write("Set the width> ")
  59. width = io.read('*n')
  60. io.write("Set the height> ")
  61. height = io.read('*n')
  62. io.write("Set the turns> ")
  63. turns = io.read('*n')
  64.  
  65. io.write("Write the solution in ",height," lines please\n")
  66. for i=1,height,1 do
  67. spanel[i] = io.read('*l')
  68. end
  69.  
  70. io.write("\nWrite the puzzle in ",height," lines please\n")
  71. for i=1,height,1 do
  72. ppanel[i] = io.read('*l')
  73. end
  74.  
  75. print("\nSo the puzzle is: ")
  76. for i=1,height,1 do
  77. print(ppanel[i])
  78. end
  79.  
  80. print("\nSo the solution is: ")
  81. for i=1,height,1 do
  82. print(spanel[i])
  83. end
  84. --[[
  85. str = touch(ppanel,2,2)
  86. for i=1,height,1 do
  87. print(str[i])
  88. end
  89. ]]--
  90. --[[
  91. str = ppanel
  92. while(panelcmp(spanel,ppanel) == false) do panels = panels + 1
  93. io.write("x> ")
  94. x = io.read('*n')
  95. io.write("y> ")
  96. y = io.read('*n')
  97. str = touch(str,y,x);
  98. show(str)
  99. if (panels > turns) then panels = 0; str = ppanel; end
  100. end]]--
  101. print(panelcmp(spanel,ppanel))
  102.  
  103. bruteforce(spanel,ppanel)
RAW Paste Data