-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
124 lines (114 loc) · 3.01 KB
/
index.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
import run from "aocrunner";
import * as fs from "node:fs";
const parseInput = (rawInput: string) =>
rawInput.split("\n").map((line) => {
const values = line.match(/p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)/)!;
return {
p: {
x: Number(values[1]),
y: Number(values[2]),
},
v: {
x: Number(values[3]),
y: Number(values[4]),
},
};
});
const part1 = (rawInput: string) => {
const initialRobots = parseInput(rawInput);
const isTest = initialRobots.length < 100;
const width = isTest ? 11 : 101;
const height = isTest ? 7 : 103;
const endRobots = initialRobots.map(({ p, v }) => ({
x: (((p.x + v.x * 100) % width) + width) % width,
y: (((p.y + v.y * 100) % height) + height) % height,
}));
const firstQuadrantCount = endRobots.filter(
({ x, y }) => x < Math.floor(width / 2) && y < Math.floor(height / 2),
).length;
const secondQuadrantCount = endRobots.filter(
({ x, y }) => x < Math.floor(width / 2) && y > Math.floor(height / 2),
).length;
const thirdQuadrantCount = endRobots.filter(
({ x, y }) => x > Math.floor(width / 2) && y < Math.floor(height / 2),
).length;
const fourthQuadrantCount = endRobots.filter(
({ x, y }) => x > Math.floor(width / 2) && y > Math.floor(height / 2),
).length;
// console.log(firstQuadrantCount,secondQuadrantCount,thirdQuadrantCount,fourthQuadrantCount)
return (
firstQuadrantCount *
secondQuadrantCount *
thirdQuadrantCount *
fourthQuadrantCount
);
};
const part2 = (rawInput: string) => {
let robots = parseInput(rawInput);
const width = 101;
const height = 103;
for (let step = 1; step < 10000; step++) {
robots.forEach(({ p, v }) => {
p.x = (((p.x + v.x) % width) + width) % width;
p.y = (((p.y + v.y) % height) + height) % height;
});
const columnCheck = Array(width)
.fill(null)
.map((_, i) => i)
.some((x) => robots.filter(({ p }) => p.x === x).length > 30);
const rowCheck = Array(height)
.fill(null)
.map((_, i) => i)
.some((y) => robots.filter(({ p }) => p.y === y).length > 20);
if (rowCheck&&columnCheck) {
return step;
// let image = "";
// image += `Step ${step}\n\n`;
// for (let y = 0; y < height; y++) {
// let row = "";
// for (let x = 0; x < width; x++) {
// if (robots.some(({ p }) => p.x === x && p.y === y)) {
// row += "#";
// } else {
// row += " ";
// }
// }
// image += row + "\n";
// console.log(image);
// }
}
}
};
run({
part1: {
tests: [
{
input: `p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3`,
expected: 12,
},
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});