-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.ts
148 lines (134 loc) · 3.63 KB
/
11.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { SUM, isPresent } from "../utils/helpers.js";
import Task, { TaskPartSolution } from "../utils/task.js";
type Char = "." | "#";
type Universe = Char[][];
const parseUniverse = (input: string): Universe => {
return input.split("\n").map((row) => row.split("") as Char[]);
};
const getEmpties = (universe: Universe): { emptyRows: Set<number>; emptyCols: Set<number> } => {
const emptyRows = new Set<number>(
universe.map((r, i) => (r.every((n) => n === ".") ? i : undefined)).filter(isPresent)
);
const emptyCols = new Set<number>();
for (let i = 0; i < universe[0].length; i++) {
const col = universe.map((r) => r[i]);
if (col.every((n) => n === ".")) {
emptyCols.add(i);
}
}
return { emptyRows, emptyCols };
};
const expandUniverse = (universe: Universe) => {
const { emptyCols, emptyRows } = getEmpties(universe);
const newUniverse: Universe = [];
universe.forEach((r, i) => {
const newR: Char[] = [];
r.forEach((n, i) => {
newR.push(n);
if (emptyCols.has(i)) {
newR.push(".");
}
});
newUniverse.push(newR);
if (emptyRows.has(i)) {
newUniverse.push([...newR]);
}
});
return newUniverse;
};
interface Pos {
x: number;
y: number;
}
const dist = (a: Pos, b: Pos) => Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
const solve1 = (universe: Universe) => {
const gals: Pos[] = [];
for (let y = 0; y < universe.length; y++) {
for (let x = 0; x < universe[y].length; x++) {
if (universe[y][x] === "#") {
gals.push({ x, y });
}
}
}
const lengths: number[] = [];
for (const g of gals) {
for (const h of gals) {
lengths.push(dist(g, h));
}
}
return SUM(lengths) / 2;
};
const part1: TaskPartSolution = (input) => {
const universe = parseUniverse(input);
// const expanded = expandUniverse(universe);
return solve2(universe, 2);
};
const dist2 = (a: Pos, b: Pos, emptyCols: Set<number>, emptyRows: Set<number>, m: number): number => {
const ec = Array.from(emptyCols);
const er = Array.from(emptyRows);
const minx = Math.min(a.x, b.x);
const maxx = Math.max(a.x, b.x);
const miny = Math.min(a.y, b.y);
const maxy = Math.max(a.y, b.y);
const emptyx = ec.filter((n) => n > minx && n < maxx).length;
const emptyy = er.filter((n) => n > miny && n < maxy).length;
const dx = Math.abs(a.x - b.x);
const dy = Math.abs(a.y - b.y);
const res = dx - emptyx + (dy - emptyy) + emptyx * m + emptyy * m;
// console.log({ m, a, b, minx, maxx, miny, maxy, ec, er, emptyx, emptyy, dx, dy, res });
return res;
};
const solve2 = (universe: Universe, m: number): number => {
const { emptyCols, emptyRows } = getEmpties(universe);
const gals: Pos[] = [];
for (let y = 0; y < universe.length; y++) {
for (let x = 0; x < universe[y].length; x++) {
if (universe[y][x] === "#") {
gals.push({ x, y });
}
}
}
const lengths: number[] = [];
for (const g of gals) {
for (const h of gals) {
lengths.push(dist2(g, h, emptyCols, emptyRows, m));
}
}
return SUM(lengths) / 2;
};
const part2: TaskPartSolution = (input) => {
const universe = parseUniverse(input);
// const expanded = expandUniverse(universe);
return solve2(universe, 1_000_000);
};
const task = new Task(2023, 11, part1, part2, {
part1: {
input: `...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....`,
result: "374",
},
part2: {
input: `...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....`,
result: "1030",
},
});
export default task;
// X: 159730917863
// start ~6:02, end: 6:44