-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
89 lines (79 loc) · 2.29 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
import run from "aocrunner";
const parseInput = (rawInput: string) => {
const lines = rawInput.split("\n");
const times = Array.from(lines[0].split(":")[1]!!.matchAll(/\d+/g)).map(match => Number.parseInt(match[0]));
const distances = Array.from(lines[1].split(":")[1]!!.matchAll(/\d+/g)).map(match => Number.parseInt(match[0]));
return { times, distances };
}
const part1 = (rawInput: string) => {
const { times, distances } = parseInput(rawInput);
// console.log(times)
// console.log(distances)
const raceResults = [];
for (let i = 0; i < times.length; i++) {
const raceTime = times[i];
const raceDistance = distances[i];
let winningMethods = 0;
for (let holdTime = 1; holdTime < raceTime; holdTime++) {
const timeLeft = raceTime - holdTime;
const distanceTraveled = holdTime * timeLeft;
if (distanceTraveled > raceDistance) {
winningMethods++;
}
}
raceResults.push(winningMethods);
}
console.log(raceResults);
return raceResults.reduce((previousValue, currentValue) => previousValue * currentValue, 1);
};
const part2 = (rawInput: string) => {
const { times, distances } = parseInput(rawInput);
const raceResults = [];
const raceTime = Number.parseInt(times.reduce((prev, curr) => prev + curr, ""));
const raceDistance = Number.parseInt(distances.reduce((prev, curr) => prev + curr, ""));
// console.log(raceTime)
// console.log(raceDistance)
let winningMethods = 0;
for (let holdTime = 1; holdTime < raceTime; holdTime++) {
const timeLeft = raceTime - holdTime;
const distanceTraveled = holdTime * timeLeft;
if (distanceTraveled > raceDistance) {
winningMethods++;
}
}
raceResults.push(winningMethods);
// console.log(raceResults);
return raceResults.reduce((previousValue, currentValue) => previousValue * currentValue, 1);
};
run({
part1: {
tests: [
{
input: `
Time: 7 15 30
Distance: 9 40 200`,
expected: 288,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `
Time: 71530
Distance: 940200`,
expected: 71503,
},
{
input: `
Time: 7 15 30
Distance: 9 40 200`,
expected: 71503,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});