r/adventofcode • • Dec 21 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 21 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • Community fun event 2023: ALLEZ CUISINE!

    • Submissions megathread is now unlocked!
    • 2 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

AoC Community Fun 2023: ALLEZ CUISINE!

Both today and tomorrow's secret ingredient is… *whips off cloth covering and gestures grandly*

Omakase! (Chef's Choice)

Omakase is an exceptional dining experience that entrusts upon the skills and techniques of a master chef! Craft for us your absolute best showstopper using absolutely any secret ingredient we have revealed for any day of this event!

  • Choose any day's special ingredient and any puzzle released this year so far, then craft a dish around it!
  • Cook, bake, make, decorate, etc. an IRL dish, craft, or artwork inspired by any day's puzzle!

OHTA: Fukui-san?
FUKUI: Go ahead, Ohta.
OHTA: The chefs are asking for clarification as to where to put their completed dishes.
FUKUI: Ah yes, a good question. Once their dish is completed, they should post it in today's megathread with an [ALLEZ CUISINE!] tag as usual. However, they should also mention which day and which secret ingredient they chose to use along with it!
OHTA: Like this? [ALLEZ CUISINE!][Will It Blend?][Day 1] A link to my dish…
DR. HATTORI: You got it, Ohta!
OHTA: Thanks, I'll let the chefs know!

ALLEZ CUISINE!

Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!


--- Day 21: Step Counter ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 01:19:03, megathread unlocked!

34 Upvotes

380 comments sorted by

View all comments

Show parent comment

5

u/scibuff Dec 21 '23

The polynomial interpolation is actually quite simple for x^2 with just 3 points (with xs being 0, 1 and 2)

/**
 * Lagrange's Interpolation formula for ax^2 + bx + c with x=[0,1,2] and y=[y0,y1,y2] we have
 *   f(x) = (x^2-3x+2) * y0/2 - (x^2-2x)*y1 + (x^2-x) * y2/2
 * so the coefficients are:
 * a = y0/2 - y1 + y2/2
 * b = -3*y0/2 + 2*y1 - y2/2
 * c = y0
 */
const simplifiedLagrange = (values) => {
  return {
    a: values[0] / 2 - values[1] + values[2] / 2,
    b: -3 * (values[0] / 2) + 2 * values[1] - values[2] / 2,
    c: values[0],
  };
};

So for example

console.log(simplifiedLagrange([3751, 33531, 92991])
// { a: 14840, b: 14940, c: 3751 }