-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.ts
133 lines (106 loc) · 2.62 KB
/
day14.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
import { input } from './input';
// size of the test input
// const width = 11;
// const height = 7;
const width = 101;
const height = 103;
type Point = [number, number];
const myGuards = input.split('\n').map((r) => {
const [p, v] = r.split(' ');
let idx = p.indexOf(',');
const start: Point = [
Number(p.substring(2, idx)),
Number(p.substring(idx + 1)),
];
idx = v.indexOf(',');
const dir: Point = [
Number(v.substring(2, idx)),
Number(v.substring(idx + 1)),
];
return { start, dir };
});
function part1(guards: { start: Point; dir: Point }[]) {
const time = 100;
const result = new Array(4).fill(0);
const midX = Math.floor(width / 2);
const midY = Math.floor(height / 2);
function findQuad(x: number, y: number) {
let q = x < midX ? 0 : x > midX ? 1 : -99;
if (q < 0) {
return;
}
q += y < midY ? 0 : y > midY ? 2 : -99;
if (q < 0) {
return;
}
result[q]++;
}
for (let { start: p, dir: v } of guards) {
const netX = (v[0] * time) % width;
const netY = (v[1] * time) % height;
let newX = p[0] + netX;
if (newX < 0) {
newX += width;
}
if (newX >= width) {
newX = (newX + width) % width;
}
let newY = p[1] + netY;
if (newY < 0) {
newY += height;
}
if (newY >= height) {
newY = (newY + height) % height;
}
findQuad(newX, newY);
}
return result.reduce((a, c) => (a *= c));
}
function part2(guards: { start: Point; dir: Point }[]) {
const time = 10_000;
const grid: string[][] = new Array(height);
for (let i = 0; i < grid.length; i++) {
grid[i] = new Array(width).fill('.');
}
let t = 1;
while (t <= time) {
// reset grid every iteration
for (let i = 0; i < grid.length; i++) {
grid[i] = new Array(width).fill('.');
}
for (let g of guards) {
const { start: p, dir: v } = g;
let newX = p[0] + v[0];
if (newX < 0) {
newX += width;
}
if (newX >= width) {
newX = newX - width;
}
let newY = p[1] + v[1];
if (newY < 0) {
newY += height;
}
if (newY >= height) {
newY = newY - height;
}
g.start = [newX, newY];
grid[newY][newX] = '#';
}
const strGrid = grid.map((r) => r.join(''));
const hasLine = strGrid.some((r) =>
// after manual inspection we know this is the bottom
// of the tree
r.includes('###############################'),
);
if (hasLine) {
for (let r of strGrid) {
console.log(r);
}
return t;
}
t++;
}
}
console.log(part1(myGuards));
console.log(part2(myGuards));