-
Notifications
You must be signed in to change notification settings - Fork 0
/
beach.js
35 lines (29 loc) · 1.16 KB
/
beach.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const map = [
['*', 8, '-', 1],
[4, '*', 11, '*'],
['+', 4, '-', 18],
[22, '-', 9, '*']]
const directions = [{x: 0, y: -1, v: 'north'}, {x: 0, y: 1, v: 'south'},
{x: -1, y: 0, v: 'west'}, {x: 1, y: 0, v: 'east'}];
let start = {x:0, y:3}, end = {x:3, y:0}, maxSteps = 15, i = 0,
paths = [{x: start.x, y: start.y, val: 0, op: '+', steps: []}];
while (paths[i]) {
let p = paths[i++], val = map[p.y][p.x], expand = true;
if (isNaN(val)) p.op = val;
else if (p.op == '+') p.val += val;
else if (p.op == '-') p.val -= val;
else if (p.op == '*') p.val *= val;
if (p.val < -20 || p.val > 120) expand = false;
if (p.x == start.x && p.y == start.y && p.steps[0]) expand = false;
if (p.x == end.x && p.y == end.y) {
expand = false;
if (p.val == 30 && p.steps.length < maxSteps) {
console.log('shortest trip found', p.steps, p);
maxSteps = p.steps.length;
}
}
if (expand) directions.map(d => {
let np = {x: p.x+d.x, y: p.y+d.y, val: p.val, op: p.op, steps: [...p.steps, d.v]}
if (map[np.y] && map[np.y][np.x] && np.steps.length < maxSteps) paths.push(np);
})
}