Public
Edited
Dec 6, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
lines = input.split("\n")
Insert cell
times = lines[0].match(/\d+/g).map(Number)
Insert cell
distances = lines[1].match(/\d+/g).map(Number)
Insert cell
Insert cell
Insert cell
distance = (n, time) => n * (time - n)
Insert cell
Insert cell
record = (time, winDistance) =>
_.range(0, time).filter((t) => distance(t, time) > winDistance)
Insert cell
Insert cell
part1 = _.zip(times, distances)
.flatMap(([t, d]) => record(t, d).length)
.reduce((a, b) => a * b, 1)
Insert cell
Insert cell
Insert cell
Insert cell
realTime = Number(times.join(""))
Insert cell
realDistance = Number(distances.join(""))
Insert cell
Insert cell
roots = quadratic(1, -realTime, realDistance)
Insert cell
part2 = {
const [root1, root2] = roots;
return Math.floor(root1) - Math.ceil(root2) + (root1 % 1 !== 0);
}
Insert cell
// Bruteforce solution
// part2 = record(realTime, realDistance).length
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
vizCoords = {
yield _.range(0, vizTime + 1).map((t, i) =>
[Math.max(distance(t, currentTime), 0), i]
);
}
Insert cell
vizRoots = quadratic(1, -vizTime, vizDistance)
Insert cell
curve = _.map(
build_samples(
(x) => -(x ** 2) + vizTime * x - vizDistance + vizDistance,
0,
vizTime
),
_.reverse
)
Insert cell
quadratic = (a, b, c) => {
const discriminant = Math.sqrt(b ** 2 - 4 * a * c);
return [(-b + discriminant) / (2 * a), (-b - discriminant) / (2 * a)];
}
Insert cell
Insert cell
import { build_samples } from "@mcmcclur/adaptive-plotter"
Insert cell
input = textarea || select.value
Insert cell
testInput = `Time: 7 15 30
Distance: 9 40 200`
Insert cell
problemInput = `Time: 53 91 67 68
Distance: 250 1330 1081 1025`
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more