-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.ts
132 lines (117 loc) · 3.04 KB
/
day6.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
const test_input = `Time: 7 15 30
Distance: 9 40 200`.split('\n');
const puzzle_input = `<get from aoc website>`.split('\n');
function log(u: unknown) {
console.log(JSON.stringify(u));
}
function numStringToArray(src: string): number[] {
return src
.split(' ')
.filter((s) => s.trim().length > 0)
.map(Number);
}
function part1(input: string[]) {
const times = numStringToArray(input[0].split(':')[1]);
const distances = numStringToArray(input[1].split(':')[1]);
const distance = (t: number, limit: number) => t * (limit - t);
const midpoint = (limit: number) => limit / 2;
const cnt: number[] = [];
for (let i = 0; i < times.length; i++) {
// find valid start
let center = Math.floor(midpoint(times[i]));
// left edge
let l = 0;
let r = center;
let d = 0;
let m = -1;
let leftEdge = Number.MAX_VALUE;
while (l <= r) {
m = Math.floor((l + r) / 2);
d = distance(m, times[i]);
log({ i, l, r, m, d });
if (d < distances[i]) {
l = m + 1;
} else if (d > distances[i]) {
leftEdge = Math.min(m, leftEdge);
r = m - 1;
} else {
leftEdge = Math.min(m + 1, leftEdge);
break;
}
}
l = center;
r = times[i];
d = 0;
m = -1;
let rightEdge = Number.MIN_VALUE;
while (l <= r) {
m = Math.floor((l + r) / 2);
d = distance(m, times[i]);
log({ i, l, r, m, d });
if (d > distances[i]) {
rightEdge = Math.max(m, rightEdge);
l = m + 1;
} else if (d < distances[i]) {
r = m - 1;
} else {
rightEdge = Math.max(m - 1, rightEdge);
break;
}
}
log({ leftEdge, rightEdge });
cnt.push(rightEdge - leftEdge + 1);
}
const result = cnt.reduce((a, c) => a * c);
log(result);
}
function part2(input: string[]) {
const time = Number(input[0].split(':')[1].replaceAll(' ', ''));
const target = Number(input[1].split(':')[1].replaceAll(' ', ''));
log({ time, target });
const distance = (t: number, limit: number) => t * (limit - t);
const midpoint = (limit: number) => limit / 2;
// find valid start
let center = Math.floor(midpoint(time));
// left edge
let l = 0;
let r = center;
let d = 0;
let m = -1;
let leftEdge = Number.MAX_VALUE;
while (l <= r) {
m = Math.floor((l + r) / 2);
d = distance(m, time);
log({ l, r, m, d });
if (d < target) {
l = m + 1;
} else if (d > target) {
leftEdge = Math.min(m, leftEdge);
r = m - 1;
} else {
leftEdge = Math.min(m + 1, leftEdge);
break;
}
}
l = center;
r = time;
d = 0;
m = -1;
let rightEdge = Number.MIN_VALUE;
while (l <= r) {
m = Math.floor((l + r) / 2);
d = distance(m, time);
log({ l, r, m, d });
if (d > target) {
rightEdge = Math.max(m, rightEdge);
l = m + 1;
} else if (d < target) {
r = m - 1;
} else {
rightEdge = Math.max(m - 1, rightEdge);
break;
}
}
log({ leftEdge, rightEdge });
log(rightEdge - leftEdge + 1);
}
part2(puzzle_input);