Skip to content

Commit 4568bb3

Browse files
committed
feat(day11): rotate the map
1 parent 0627b4a commit 4568bb3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

day11_test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { assertEquals } from "https://deno.land/std@0.208.0/assert/assert_equals.ts";
2+
3+
Deno.test("Day 11: Cosmic Expansion", async (t) => {
4+
await t.step(
5+
"rotate map",
6+
async (t) => {
7+
await t.step("rotateLeft()", () =>
8+
assertEquals(
9+
rotateLeft([
10+
`123`,
11+
`456`,
12+
]),
13+
[
14+
`36`,
15+
`25`,
16+
`14`,
17+
],
18+
));
19+
20+
await t.step("rotateRight()", () =>
21+
assertEquals(
22+
rotateRight([
23+
`36`,
24+
`25`,
25+
`14`,
26+
]),
27+
[
28+
`123`,
29+
`456`,
30+
],
31+
));
32+
},
33+
);
34+
});
35+
36+
const rotateLeft = (map: Array<string>): Array<string> => {
37+
const res: Array<Array<string>> = [];
38+
const w = map[0].length;
39+
for (let col = 0; col < w; col++) {
40+
for (let row = 0; row < map.length; row++) {
41+
const i = w - col - 1;
42+
if (res[i] === undefined) res[i] = [];
43+
res[i][row] = map[row][col];
44+
}
45+
}
46+
47+
return res.map((row) => row.join(""));
48+
};
49+
50+
const rotateRight = (map: Array<string>): Array<string> =>
51+
rotateLeft(rotateLeft(rotateLeft(map)));

0 commit comments

Comments
 (0)