-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.ts
78 lines (66 loc) · 2.16 KB
/
day11.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 { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";
import { loadTestData, loadData } from "./utils.ts";
const part1test = 374; // fill this in
const part2test10 = 1030;
const part2test100 = 8410;
const pairs = <T>(array: T[]): T[][] => {
const pairs = [];
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
pairs.push([array[i], array[j]])
}
}
return pairs;
}
const solution = (data: string, factor: number) => {
const grid = data.split('\n');
const emptyRows = [];
for (let i = 0; i < grid.length; i++) {
const line = grid[i];
if (!line.includes("#")) {
emptyRows.push(i);
}
};
const emptyColumns = [];
for (let i = 0; i < grid[0].length; i++) {
let found = false;
for (let j = 0; j < grid.length; j++) {
if (grid[j][i] == '#') {
found = true;
break;
}
}
if (!found) {
emptyColumns.push(i);
}
}
const galaxies = [];
for (let i = 0; i < grid[0].length; i++) {
for (let j = 0; j < grid.length; j++) {
if (grid[j][i] == '#') {
galaxies.push([i, j]);
}
}
}
const galaxyPairs = pairs(galaxies);
let sum = 0;
for (const pair of galaxyPairs) {
for (let i = Math.min(pair[0][0], pair[1][0]); i < Math.max(pair[0][0], pair[1][0]); i++) {
if (emptyColumns.includes(i)) {
sum += factor - 1;
}
}
for (let i = Math.min(pair[0][1], pair[1][1]); i < Math.max(pair[0][1], pair[1][1]); i++) {
if (emptyRows.includes(i)) {
sum += factor - 1;
}
}
sum += Math.abs(pair[0][0] - pair[1][0]) + Math.abs(pair[0][1] - pair[1][1]);
}
return sum;
};
assertEquals(solution(await loadTestData(11), 2), part1test)
console.log(solution(await loadData(11), 2))
assertEquals(solution(await loadTestData(11), 10), part2test10)
assertEquals(solution(await loadTestData(11), 100), part2test100)
console.log(solution(await loadData(11), 1000000))