-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.ts
60 lines (54 loc) · 1.65 KB
/
06.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
interface Race {
time: number;
dist: number;
}
// Parse the lines as records for different races
// Time: 7 15 30
// Distance: 9 40 200
// -> [{ time: 7, dist: 9 }, { time: 15, dist: 40 }, ...]
function parseRaces(input: string): Race[] {
const lineNums = input.split("\n")
.map((line) =>
Array.from(line.match(/\d+/g) ?? [])
.map((digits) => parseInt(digits))
);
return lineNums[0].map((time, raceIndex) => {
return { time, dist: lineNums[1][raceIndex] };
});
}
// Parse the lines as the record for a single race
// Time: 7 15 30
// Distance: 9 40 200
// -> { time: 71530, dist: 940200 }
function parseRealRace(input: string): Race {
const [time, dist] = input.split("\n").map((line) =>
parseInt(line.replace(/\D+/g, ""))
);
return { time, dist };
}
function winErrorMargin(race: Race) {
return new Array(race.time).fill(race.time).map(
(maxTime, time) => {
return time * (maxTime - time);
},
).filter((dist) => dist > race.dist).length;
}
const distance = (holdTime: number, totalTime: number) =>
(totalTime - holdTime) * holdTime;
// Find total number of ways to beat the records
export function part1(input: string) {
const races = parseRaces(input);
const margins = races.map(winErrorMargin);
return margins.reduce((acc, margin) => acc * margin);
}
// Find total number of ways to beat the single long record
export function part2(input: string) {
const race = parseRealRace(input);
// Crude brute force solution, test all times
let wins = 0;
for (let i = 0; i <= race.time; i++) {
const dist = distance(i, race.time);
if (dist > race.dist) wins++;
}
return wins;
}