From 1ae9c4896a9df369f59570c9f1e0e0cb6d2eb745 Mon Sep 17 00:00:00 2001 From: Asraelite Date: Thu, 15 Jan 2015 19:56:04 +0000 Subject: [PATCH] init --- README.md | 27 +++++++++++++++++ index.html | 10 +++++++ script.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 README.md create mode 100644 index.html create mode 100644 script.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..97ea889 --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# 26-solver +Simple Javascript script that will find solutions to the puzzle "26". + +The Puzzle +---------- + +A game board consists of 12 holes into which you place 12 small pieces which have the numbers 1 through 12 marked on them. The holes on the board are arranged in a star pattern as follows: + + () + / \ + ()---()----()---() + \ / \ / + () () + / \ / \ + ()----()----()----() + \ / + () + +The aim of the puzzle is to arrange the 12 pieces so that each of the 6 lines on the board sum up to 26. + +There are 80 possible solutions, not counting rotations and mirrors, 960 counting them. + +How to Use +---------- + +Open index.html and click the solve button. All possible solutions will be printed in the javascript console. + diff --git a/index.html b/index.html new file mode 100644 index 0000000..1b92670 --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/script.js b/script.js new file mode 100644 index 0000000..ebc71b8 --- /dev/null +++ b/script.js @@ -0,0 +1,88 @@ +// Add event listener to button to solve when clicked. +window.onload = function() { + document.getElementById('go').addEventListener('click', solve); +} + +/* + * The layout of the game board is as follows: + * + * 0 + * 1 2 3 4 + * 5 6 + * 7 8 9 10 + * 11 + * + * The puzzle is to find an arrangement of numbers + * so that every line adds up to 26. + */ + +var solutions = []; + +function solve() { + // Call recursive cyclePlace function on an empty starting arragement ([]). + cyclePlace(0, []); + + console.log(solutions.length + ' solutions found'); +} + +// Function that accepts an incomplete arrangement and calls itself 12 times for +// each possible value of the next part of the arrangement added on to the current one. +// Each one of these calls then calls 12 more for the next arrangement and so on, totalling +// 12^n calls needed, which is reduced by early checks. A call at depth 11 performs a test +// on the given complete arrangement and adds it to the list of solutions if correct. +function cyclePlace(n, prev) { + // Stop early if first checkable line is invalid. This drastically improves speed. + if(n == 5) { + if(prev[1] + prev[2] + prev[3] + prev[4] != 26) { + return; + } + } + + // Again, but for second checkable line. + if(n == 8) { + if(prev[0] + prev[2] + prev[5] + prev[7] != 26) { + return; + } + } + + // More checks could be added here but the program is fast enough as-is. + + // Perform 12 deeper calls for each of the possible permutations of the next arrangement. + for(var i = 1; i <= 12; i++) { + if(prev.indexOf(i) == -1) { + // If deepest call. + if(n == 11) { + if(test(prev.concat(i))) { + // Add arrangement to solutions if correct. + solutions.push(prev.concat(i)); + console.info('SOLUTION: ' + prev.concat(i)); + }; + } else { + cyclePlace(n + 1, prev.concat(i)); + } + } + } +} + +// Test is given arrangement is correct. +function test(order) { + // The indices of the numbers that make up each line. + var lines = [ + [0, 2, 5, 7], + [1, 5, 8, 11], + [7, 8, 9, 10], + [11, 9, 6, 4], + [10, 6, 3, 0], + [4, 3, 2, 1] + ]; + + // Cycle through each line and ensure its sum is 26. + for(var i = 0; i < 6; i++) { + var line = lines[i]; + if(order[line[0]] + order[line[1]] + order[line[2]] + order[line[3]] != 26) return false; + } + + // Return true if no incorrect line caused a return false first. + return true; +} +