Day 6 done

This commit is contained in:
Simon Cambier 2023-12-07 12:57:22 +01:00
parent 75d5cb8943
commit 8b1cda63b1
2 changed files with 68 additions and 11 deletions

View File

@ -18,40 +18,35 @@ const times = rtimes.filter((o) => o !== "").map((o) => Number(o));
const [d, ...rdist] = input[1].split(" "); const [d, ...rdist] = input[1].split(" ");
const dist = rdist.filter((o) => o !== "").map((o) => Number(o)); const dist = rdist.filter((o) => o !== "").map((o) => Number(o));
let mul = 1;
for (let i = 0; i < times.length; i++) { for (let i = 0; i < times.length; i++) {
races.push({ time: times[i], distance: dist[i] }); races.push({ time: times[i], distance: dist[i] });
} }
console.log(races); console.log(races);
function getSpeed(race: Race, holdTime: number): number { function getDistance(race: Race, holdTime: number): number {
return (race.time - holdTime) * holdTime; return (race.time - holdTime) * holdTime;
} }
const counts: number[] = [];
for (const race of races) { for (const race of races) {
let from = 1; let from = 1;
let to = 1; let to = 1;
for (let i = 0; i < race.time; i++) { for (let i = 0; i < race.time; i++) {
if (getSpeed(race, i) > race.distance) { if (getDistance(race, i) > race.distance) {
from = i; from = i;
break; break;
} }
} }
for (let i = race.time; i >= 0; i--) { for (let i = race.time; i >= 0; i--) {
if (getSpeed(race, i) > race.distance) { if (getDistance(race, i) > race.distance) {
to = i; to = i;
break; break;
} }
} }
counts.push(to - from + 1); mul *= to - from + 1;
} }
let mul = 1;
for (const count of counts) {
mul *= count;
}
console.log(mul); console.log(mul);
const timeTotal = new Date().getTime() - timeFrom; const timeTotal = new Date().getTime() - timeFrom;

62
day6_plot.p8 Normal file
View File

@ -0,0 +1,62 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__lua__
local races = {
{ 7, 9 },
{ 15, 40 },
{ 30, 200 }
}
printh("")
printh("aoc 2023 6")
printh("")
function get_dist(race, t)
return (race[1] - t) * t
end
function trace()
line(1, 126, 127, 126, 13)
line(1, 126, 1, 1, 13)
print("distance", 3, 1, 8)
local x, y
for race in all(races) do
local md = 0 ym = 0 xm = 0 bt = 0 pd = 0
line(2, 126 - race[2] / 2, 127, 126 - race[2] / 2, 2)
print("time", 80, 114, 15)
print("pressed", 80, 121, 15)
line()
for i = 0, race[1] do
local dist = get_dist(race, i)
x = 1 + i * 4
y = 126 - dist / 2
if dist > md then
md = dist xm = x ym = y
end
line(x, y, 7)
yield()
if pd > dist and bt == 0 then bt = i - 1 end
pd = dist
end
print(md .. "MM", xm - 2, ym - 6, 8)
print(bt .. "MS", xm + 20, ym - 6, 15)
-- print(race[2],50,126-race[2]/2-6,2)
end
end
local co = cocreate(trace)
cls()
function _update()
coresume(co)
end
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000