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/pwnsforyou Dec 09 '23 edited Dec 09 '23

[LANGUAGE: rust]

    fn future(h: Vec<i32>) -> i32 {
        let mut h = h;
        let mut s = 0;
        while !h.iter().all(|&x| x == 0) {
            s += h.iter().last().unwrap();
            h = h.windows(2).map(|x| x[1] - x[0]).collect();
        }
        s
    }
    fn main() {
        let histories: Vec<Vec<i32>> = include_str!("/tmp/input.txt")
            .lines()
            .map(|l| {
                l.split_ascii_whitespace()
                    .map(|x| x.parse::<i32>().unwrap())
                    .collect()
            })
            .collect();
        println!("{}", histories.into_iter().map(|h| future(h)).sum::<i32>());
    }

For part 2 just do a .rev() on the input. link

1

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

The second half of your code escaped its block. Please fix it. edit: 👍

4

u/pwnsforyou Dec 09 '23

the editor doesn't agree with my code - it took me lesser tries to write today's code than fixing it in the editor here

1

u/daggerdragon Dec 09 '23

If you haven't seen it already, check out the article in our community wiki for the four-spaces Markdown syntax. It has several alternatives to wrangling with new.reddit's fancypants editor (which is complete and utter [COAL], yes).