Day 6 solutions 🔧🔧🔧

This commit is contained in:
Peter 2023-12-06 14:48:18 +08:00
parent ef171aaa30
commit 6019998a79
3 changed files with 97 additions and 0 deletions

23
day_6/part_1.ts Normal file
View File

@ -0,0 +1,23 @@
import * as fs from "fs";
import { Race, solve_all_races } from "./solver";
export const read_file = () => {
const input = fs.readFileSync("input", "ascii");
const [times_str, distances_str] = input.split("\n");
const times = times_str
.split(" ")
.map((token) => Number.parseInt(token))
.filter((token) => !isNaN(token));
const distances = distances_str
.split(" ")
.map((token) => Number.parseInt(token))
.filter((token) => !isNaN(token));
const races: Race[] = times.map((time, i) => {
return { time: time, distance: distances[i] };
});
return races;
};
console.log(solve_all_races(read_file()));

25
day_6/part_2.ts Normal file
View File

@ -0,0 +1,25 @@
import * as fs from "fs";
import { Race, solve_all_races } from "./solver";
export const read_file = () => {
const input = fs.readFileSync("input", "ascii");
const [times_str, distances_str] = input.split("\n");
const times = times_str
.replace(/ /g, "")
.split(":")
.map((token) => Number.parseInt(token))
.filter((token) => !isNaN(token));
const distances = distances_str
.replace(/ /g, "")
.split(":")
.map((token) => Number.parseInt(token))
.filter((token) => !isNaN(token));
const races: Race[] = times.map((time, i) => {
return { time: time, distance: distances[i] };
});
return races;
};
console.log(solve_all_races(read_file()));

49
day_6/solver.ts Normal file
View File

@ -0,0 +1,49 @@
export interface Race {
time: number;
distance: number;
}
const try_soln = (time: number, race: Race) =>
time * (race.time - time) > race.distance;
// In[60]:= distance[ta_, tmax_] := ta*(tmax - ta)
//
// In[103]:=
// solns[d_, tmax_] := Reduce[distance[ta, tmax] >= d + 1, ta, Integers]
//
// In[104]:= solns[d, t]
//
// Out[104]= (d | t | ta) \[Element]
// Integers && ((d < 1/4 (-4 + t^2) &&
// t/2 - 1/2 Sqrt[-4 - 4 d + t^2] <= ta <=
// t/2 + 1/2 Sqrt[-4 - 4 d + t^2]) || (d == 1/4 (-4 + t^2) &&
// ta == t/2))
const get_interval_length = (race: Race) => {
var low = Math.ceil(
race.time / 2 - (1 / 2) * Math.sqrt(race.time ** 2 - 4 * race.distance)
);
var high = Math.floor(
race.time / 2 + (1 / 2) * Math.sqrt(race.time ** 2 - 4 * race.distance)
);
// lol, lmao even.
if (!try_soln(low, race)) {
low += 1;
}
if (try_soln(low - 1, race)) {
low -= 1;
}
if (!try_soln(high, race)) {
high -= 1;
}
if (try_soln(high + 1, race)) {
high += 1;
}
return high - low + 1;
};
export const solve_all_races = (races: Race[]) =>
races
.map((race) => get_interval_length(race))
.reduce((prod, v) => prod * v, 1);