diff --git a/day6/day6-ts.ts b/day6-ts/day6.ts similarity index 80% rename from day6/day6-ts.ts rename to day6-ts/day6.ts index 6590244..74f994a 100644 --- a/day6/day6-ts.ts +++ b/day6-ts/day6.ts @@ -18,40 +18,35 @@ 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)); +let mul = 1; + 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 { +function getDistance(race: Race, holdTime: number): number { return (race.time - holdTime) * holdTime; } -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) { + if (getDistance(race, i) > race.distance) { from = i; break; } } for (let i = race.time; i >= 0; i--) { - if (getSpeed(race, i) > race.distance) { + if (getDistance(race, i) > race.distance) { to = i; break; } } - counts.push(to - from + 1); - + mul *= to - from + 1; } -let mul = 1; -for (const count of counts) { - mul *= count; -} console.log(mul); const timeTotal = new Date().getTime() - timeFrom; diff --git a/day6_plot.p8 b/day6_plot.p8 new file mode 100644 index 0000000..170704c --- /dev/null +++ b/day6_plot.p8 @@ -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