summaryrefslogtreecommitdiff
path: root/euler-golf/js/sol.js
diff options
context:
space:
mode:
authorLizzy Hunt <logan.hunt@usu.edu>2023-02-24 15:15:29 -0700
committerLizzy Hunt <logan.hunt@usu.edu>2023-02-24 15:15:29 -0700
commitd49a395dbd8b9ef78a32d14b1d69f37a299ceeac (patch)
tree9ee8868140013f580a8524b5174b75a4e29502cd /euler-golf/js/sol.js
parent445af5d0be53375355b9dad02510f6b331fd99ec (diff)
downloadsimponic.xyz-d49a395dbd8b9ef78a32d14b1d69f37a299ceeac.tar.gz
simponic.xyz-d49a395dbd8b9ef78a32d14b1d69f37a299ceeac.zip
Visual changes, fix solution not being optimal when one move away
Diffstat (limited to 'euler-golf/js/sol.js')
-rw-r--r--euler-golf/js/sol.js9
1 files changed, 7 insertions, 2 deletions
diff --git a/euler-golf/js/sol.js b/euler-golf/js/sol.js
index bc403fc..b4e527f 100644
--- a/euler-golf/js/sol.js
+++ b/euler-golf/js/sol.js
@@ -16,9 +16,14 @@ const backtrack = (local_index, depth) =>
.map((direction) => (Number(direction) ? "+" : "-"));
const sol = (target, start_from = new cx(0, 0), start_to = new cx(1, 0)) => {
- let moves = [start_to, ...construct_moves(start_from, start_to)];
- let curr_depth = 2;
+ const next_moves = construct_moves(start_from, start_to);
+ const solved_in_first_move = next_moves.findIndex((move) =>
+ cx.eq(move, target)
+ );
+ if (solved_in_first_move != -1) return backtrack(solved_in_first_move, 1);
+ let moves = [start_to, ...next_moves];
+ let curr_depth = 2;
while (curr_depth < DEPTH) {
for (let i = 0; i < Math.pow(2, curr_depth); i++) {
const direction = DIRECTION[Number(i.toString(2).at(-1))];