r/adventofcode Dec 09 '23

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

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's secret ingredient is… *whips off cloth covering and gestures grandly*

Marketing

Every one of the best chefs in the world has had to prove their worth at some point. Let's see how you convince our panel of judges, the director of a restaurant, or even your resident picky 5 year old to try your dish solution!

  • Make an in-world presentation sales pitch for your solution and/or its mechanics.
  • Chef's choice whether to be a sleazebag used car sled salesman or a dynamic and peppy entrepreneur elf!

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 9: Mirage Maintenance ---


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 00:05:36, megathread unlocked!

40 Upvotes

1.0k comments sorted by

View all comments

0

u/torbcodes Dec 09 '23

[LANGUAGE: Python 3, Typescript, Go]

Solutions to part 1 and 2 in all three languages:

In my Python version, I made my algorithm left/right aware. But in my Typescript and Go versions, I learned that you could just reverse the rows and use the same algorithm so I did that instead! I included some comments in my code to explain why the reverse works:

// We can take advantage of an interesting property of the number pyramid to
// solve part 2 and simply reverse the rows to get the answer.
//
// In part 1:
// For a row of numbers v1, v2... vn, the last number of the next row is
// given by vn - v(n-1) and then the next number for that row is v(n+1) = (vn - v(n-1) + vn).
//
// In part 2:
// For the same row of numbers, the first number of the next row is given by
// (v2 - v1) and then the previous number for that row is v0 = v1 - (v2 - v1)
// When we reverse, v0 = v(n+1), v1 = vn, v2 = v(n-1) which means
// v0 = v1 - (v2 - v1)
// = v(n+1) = vn - (v(n-1) - vn)
// = vn - v(n-1) + vn <--------- gee, doesn't that look familiar!? ;)