-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday25.js
More file actions
50 lines (46 loc) · 1.47 KB
/
day25.js
File metadata and controls
50 lines (46 loc) · 1.47 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function canMouseEat(direction, game) {
let index = [game.findIndex((x) => x.find((e) => e === 'm'))];
index = [...index, game[index[0]].findIndex((x) => x === 'm')];
let directions = {
up: (index = [], game = []) => {
for (let i = index[0] - 1; i >= 0; i--)
if (game[i][index[1]] === '*') return true;
return false;
},
down: (index = [], game = []) => {
for (let i = index[0] + 1; i < game.length; i++)
if (game[i][index[1]] === '*') return true;
return false;
},
left: (index = [], game = []) => {
for (let i = index[1] - 1; i >= 0; i--)
if (game[index[0]][i] === '*') return true;
return false;
},
right: (index = [], game = []) => {
for (let i = index[1] + 1; i < game.length; i++)
if (game[index[0]][i] === '*') return true;
return false;
},
};
return directions[direction](index, game);
}
const room = [
[' ', ' ', ' '],
[' ', ' ', 'm'],
[' ', ' ', '*'],
];
console.log(canMouseEat('up', room)); // false
console.log(canMouseEat('down', room)); // true
console.log(canMouseEat('right', room)); // false
console.log(canMouseEat('left', room)); // false
const room2 = [
['*', ' ', ' ', ' '],
[' ', 'm', '*', ' '],
[' ', ' ', ' ', ' '],
[' ', ' ', ' ', '*'],
];
console.log(canMouseEat('up', room2)); // false
console.log(canMouseEat('down', room2)); // false
console.log(canMouseEat('right', room2)); // true
console.log(canMouseEat('left', room2)); // false