-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.test.ts
78 lines (66 loc) · 2.07 KB
/
b.test.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { expect, test } from "bun:test";
function zip<T>(arr: T[][]): T[][] {
const result: T[][] = [];
for (let i = 0; i < arr[0].length; i++) {
result.push(arr.map((a) => a[i]));
}
return result;
}
function solution(input: string) {
let grid = input.split("\n").map((line) => line.split(""));
const key = JSON.stringify(grid);
const seen = new Set<string>(key);
const prev = [key];
function cycle() {
for (let i = 0; i < 4; i++) {
const zipped = zip(grid)
.map((col) => col.join(""))
.map((col) => {
const split = col.split("#").map((c) => c.split(""));
for (const seg of split) {
seg.sort((a, b) => (a < b ? 1 : -1));
}
return split
.map((seg) => seg.join(""))
.join("#")
.split("");
});
grid = zipped.map((x) => x.reverse());
}
}
let i = 0;
while (true) {
i += 1;
cycle();
const key = JSON.stringify(grid);
if (seen.has(key)) {
break;
}
seen.add(key);
prev.push(key);
}
const first = prev.indexOf(JSON.stringify(grid));
const gridString = prev[first + ((1_000_000_000 - first) % (i - first))];
grid = JSON.parse(gridString);
let ans = 0;
for (let i = 0; i < grid.length; i++) {
const j = grid.length - i;
const count = grid[i].filter((c) => c === "O").length;
ans += count * j;
}
return ans;
}
test("example", async () => {
const file = Bun.file(`${import.meta.dir}/example.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 64;
expect(actual).toBe(expected);
});
test("puzzle input", async () => {
const file = Bun.file(`${import.meta.dir}/input.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 96003;
expect(actual).toBe(expected);
});