Day 6 part 1
This commit is contained in:
parent
afb9849c3c
commit
ee6e32dd3d
54
day6/day6-ts.ts
Normal file
54
day6/day6-ts.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
type Race = {
|
||||
time: number;
|
||||
distance: number;
|
||||
};
|
||||
const races: Race[] = [];
|
||||
|
||||
const input = `Time: 47 70 75 66
|
||||
Distance: 282 1079 1147 1062`.split("\n");
|
||||
|
||||
// parse input
|
||||
const [t, ...rtimes] = input[0].split(" ");
|
||||
const times = rtimes.filter((o) => o !== "").map((o) => Number(o));
|
||||
const [d, ...rdist] = input[1].split(" ");
|
||||
const dist = rdist.filter((o) => o !== "").map((o) => Number(o));
|
||||
|
||||
for (let i = 0; i < times.length; i++) {
|
||||
races.push({ time: times[i], distance: dist[i] });
|
||||
}
|
||||
console.log(races);
|
||||
|
||||
function getSpeed(race: Race, holdTime: number): number {
|
||||
return (race.time - holdTime) * holdTime;
|
||||
}
|
||||
|
||||
let mul = 1;
|
||||
const counts: number[] = [];
|
||||
|
||||
for (const race of races) {
|
||||
let from = 1;
|
||||
let to = 1;
|
||||
for (let i = 0; i < race.time; i++) {
|
||||
if (getSpeed(race, i) > race.distance) {
|
||||
from = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (let i = race.time; i >= 0; i--) {
|
||||
if (getSpeed(race, i) > race.distance) {
|
||||
to = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
counts.push(to - from + 1);
|
||||
|
||||
}
|
||||
for (const count of counts) {
|
||||
mul *= count;
|
||||
}
|
||||
console.log(mul);
|
||||
|
||||
// console.log(getSpeed(races[0], 2));
|
||||
// console.log(getSpeed(races[0], 3));
|
||||
// console.log(getSpeed(races[0], 4));
|
||||
// console.log(getSpeed(races[0], 5));
|
Loading…
Reference in New Issue
Block a user