-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSnake-encoding.js
34 lines (28 loc) · 977 Bytes
/
Snake-encoding.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
// https://www.codingame.com/ide/puzzle/snake-encoding
const N = +readline();
let times = +readline();
let grid = [...Array(N)].map(() => readline().split(''));
while (times--) {
const helper = [];
const mutate = (source, action) => {
for (let j = 0; j < grid.length; j++) {
const temp = [];
for (let i = 0; i < grid.length; i++) {
temp.push(source[i][j]);
}
helper.push(action(j,temp));
}
};
mutate(grid,(j,col) => j % 2 === 0 ? col.reverse() : col)
const flat = helper.flat();
flat.unshift(flat.pop());
const answer = flat.reduce((a, c) => {
if (a.length === 0 || a[a.length - 1].length === grid.length) a.push([c]);
else a[a.length - 1].push(c);
return a;
}, []).map((a, i) => i % 2 === 0 ? a.reverse() : a);
helper.length = 0;
mutate(answer,(_,col) => col.join(''))
grid = helper;
}
grid.forEach((e) => console.log(e));