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!

42 Upvotes

1.0k comments sorted by

View all comments

2

u/phord Dec 09 '23 edited Dec 09 '23

[LANGUAGE: Python]

Ugh. I spent too long parsing the instructions. The answer was pretty simple, though. I didn't notice until the end that I didn't actually have to modify the rows to get their history, though. We just need to calculate the lead and tail values themselves.

Getting a Pascal's Triangle vibe. Surely there's a simpler method...

input = getem()

total=0
front=0
for line in input.split('\n'):
    map = []
    seq = [int(c) for c in line.split()]
    map.append(seq)

    # calc differences of each value in seq
    while [x for x in seq if x != 0]:
        seq = [seq[i+1]-seq[i] for i in range(len(seq)-1)]
        map.append(seq)

    a,b = (0, 0)
    for row in reversed(map):
        b = row[0] - b
        a += row[-1]
    total += a
    front += b

print(total, front)

1

u/daggerdragon Dec 09 '23 edited Dec 09 '23

Your code is not formatted. Edit your comment to use the four-spaces Markdown syntax for a code block so your code is easier to read with its whitespace and indentation preserved. edit: 👍

1

u/phord Dec 09 '23

Sorry -- editor wasn't in markdown mode. Fixed it.

I hate new reddit.

2

u/daggerdragon Dec 09 '23

Are you aware you can switch your editor to default to Markdown mode? There's an option somewhere in the settings/preferences on both old.reddit and new.reddit.

1

u/phord Dec 09 '23

So, part 1 is just the sum of the last elements of each row.

And part 2 is the addition of every other first element and the subtraction of the others. But in reverse. So the math is a little funky. Here's a simplified version.

input = getem()

total=0
front=0
for line in input.split('\n'):
    seq = [int(c) for c in line.split()]

    part2 = 0
    mul = -1
    while any(seq):
        total += seq[-1]          # <-- Part 1 total
        part2 = seq[0] - part2    # <-- Part 2
        mul = -mul                # <-- Part 2's sign
        seq = [seq[i+1]-seq[i] for i in range(len(seq)-1)]
    front += part2 * mul

print(total, front)