-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.ts
164 lines (139 loc) · 3.55 KB
/
10.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import assert from 'assert';
import lodash from 'lodash';
import get from '../api';
import {splitLines} from './utils';
import {Point2} from './types';
const subtractPoints = (p1: Point2, p2: Point2): Point2 => {
return [
p1[0] - p2[0],
p1[1] - p2[1],
];
}
const vectorLength = (vector: Point2) => {
return Math.sqrt(Math.pow(vector[0], 2) + Math.pow(vector[1], 2));
}
function getPoints(data: string): Point2[] {
return splitLines(data).map((line: string, y: number) => {
const chars: string[] = line.split('');
return chars.map((char: string, x: number) => {
if (char === '#') {
return [x, y] as Point2;
}
});
}).flat(1).filter(Boolean);
}
function findMaxPoint(points: Point2[]) {
let maxVisible = -Infinity;
let maxPoint: Point2;
points.forEach(point => {
const directions = points.filter(p => p !== point).map(otherPoint => {
const vector = subtractPoints(otherPoint, point);
return Math.atan2(vector[1], vector[0]);
});
const size = (new Set(directions)).size;
if (size > maxVisible) {
maxVisible = size;
maxPoint = point;
}
});
return {maxVisible, maxPoint};
}
const findMaxVisible = (data: string) => {
const points = getPoints(data);
let {maxVisible} = findMaxPoint(points);
return maxVisible;
}
const findVaporized = (data: string, num: number) => {
const points = getPoints(data);
let {maxVisible, maxPoint} = findMaxPoint(points);
const angles = points.filter(p => p !== maxPoint).map(otherPoint => {
const vector = subtractPoints(otherPoint, maxPoint);
return {
vector,
length: vectorLength(vector),
angle: Math.atan2(vector[0], vector[1]),
point: otherPoint,
};
});
const grouped = lodash.groupBy(angles, 'angle');
const prepared = lodash.mapValues(grouped, (points, angle) => ({
angle,
points: lodash.sortBy(points, 'length'),
}));
const sorted = lodash.sortBy(Object.values(prepared), p => -Number(p.angle));
let i = 0;
let last;
while (num--) {
while (sorted[i].points.length === 0) {
i = (i + 1) % sorted.length;
}
last = sorted[i].points.shift();
i = (i + 1) % sorted.length;
}
return last.point[0] * 100 + last.point[1];
}
assert.equal(findMaxVisible([
'.#..#',
'.....',
'#####',
'....#',
'...##'
].join('\n')), 8);
assert.equal(findMaxVisible(`.#..#..###
####.###.#
....###.#.
..###.##.#
##.##.#.#.
....###..#
..#.#..#.#
#..#.#.###
.##...##.#
.....#.#..`), '41');
assert.equal(findMaxVisible(`.#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##`), '210');
assert.equal(findVaporized(`.#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##`, 200), 802);
const run = async () => {
const data = await get('2019/day/10/input');
console.log(findMaxVisible(data));
console.log(findVaporized(data, 200));
};
if (require.main === module) {
run();
}