Advertisement
Guest User

Four dice puzzle: Postscriptum

a guest
Oct 14th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. operators = {"/","*","+","-"};
  2.  
  3. templates = {
  4.     "%s %s %s %s %s %s %s",
  5.     "(%s %s %s) %s %s %s %s",
  6.     "%s %s (%s %s %s) %s %s",
  7.     "%s %s %s %s (%s %s %s)",
  8.     "(%s %s %s) %s (%s %s %s)",
  9.     "(%s %s %s %s %s) %s %s",
  10.     "%s %s (%s %s %s %s %s)",
  11.     "((%s %s %s) %s %s) %s %s",
  12.     "(%s %s (%s %s %s)) %s %s",
  13.     "%s %s ((%s %s %s) %s %s)",
  14.     "%s %s (%s %s (%s %s %s))",
  15. };
  16.  
  17. function permutations(a,b,c,d)
  18.     return {
  19.         {a,b,c,d},
  20.         {a,b,d,c},
  21.         {a,c,b,d},
  22.         {a,c,d,b},
  23.         {a,d,c,b},
  24.         {a,d,b,c},
  25.         {b,a,c,d},
  26.         {b,a,d,c},
  27.         {b,c,a,d},
  28.         {b,c,d,a},
  29.         {b,d,c,a},
  30.         {b,d,a,c},
  31.         {c,b,a,d},
  32.         {c,b,d,a},
  33.         {c,a,b,d},
  34.         {c,a,d,b},
  35.         {c,d,a,b},
  36.         {c,d,b,a},
  37.         {d,b,c,a},
  38.         {d,b,a,c},
  39.         {d,c,b,a},
  40.         {d,c,a,b},
  41.         {d,a,c,b},
  42.         {d,a,b,c},
  43.     };
  44. end
  45.  
  46. for a = 1, 6 do
  47.     for b = a, 6 do
  48.         for c = b, 6 do
  49.             for d = c, 6 do
  50.                 local solutions = {};
  51.                 for _, permutation in ipairs(permutations(a,b,c,d)) do
  52.                     for _, template in ipairs(templates) do
  53.                         for _, op1 in ipairs(operators) do
  54.                             for _, op2 in ipairs(operators) do
  55.                                 for _, op3 in ipairs(operators) do
  56.                                     local calculation = "return " .. template:format(permutation[1],op1,permutation[2],op2,permutation[3],op3,permutation[4]);
  57.     --                              print(calculation .. " = " .. assert(load(calculation))());
  58.                                     local calc = assert(load(calculation))();
  59.                                     if calc == calc then --to check for NaN                        
  60.                                         solutions[calc] = true;
  61.                                     end
  62.                                     calculation = "return " .. template:format(permutation[1],op1,permutation[2],op2,permutation[3],"+",0);
  63.     --                              print(calculation .. " = " .. assert(load(calculation))());
  64.                                     local calc = assert(load(calculation))();
  65.                                     if calc == calc then --to check for NaN                        
  66.                                         solutions[calc] = true;
  67.                                     end
  68.                                     calculation = "return " .. template:format(permutation[1],op1,permutation[2],"+",0,"+",0);
  69.     --                              print(calculation .. " = " .. assert(load(calculation))());
  70.                                     local calc = assert(load(calculation))();
  71.                                     if calc == calc then --to check for NaN                        
  72.                                         solutions[calc] = true;
  73.                                     end
  74.                                     calculation = "return " .. template:format(permutation[1],"+",0,"+",0,"+",0);
  75.     --                              print(calculation .. " = " .. assert(load(calculation))());
  76.                                     local calc = assert(load(calculation))();
  77.                                     if calc == calc then --to check for NaN                        
  78.                                         solutions[calc] = true;
  79.                                     end
  80.                                 end
  81.                             end
  82.                         end
  83.                     end
  84.                 end
  85.                 local i = 1;
  86.                 while true do
  87.                     if not solutions[i] then
  88.                         print("N(" .. a .. "," .. b .. "," .. c .. "," .. d .. ") = " .. i);
  89.                         break
  90.                     end
  91.                     i = i + 1;
  92.                 end
  93.             end
  94.         end
  95.     end
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement