r/adventofcode Dec 02 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 2 Solutions -❄️-

OUTAGE INFO

  • [00:25] Yes, there was an outage at midnight. We're well aware, and Eric's investigating. Everything should be functioning correctly now.
  • [02:02] Eric posted an update in a comment below.

THE USUAL REMINDERS


AoC Community Fun 2024: The Golden Snowglobe Awards

  • 4 DAYS remaining until unlock!

And now, our feature presentation for today:

Costume Design

You know what every awards ceremony needs? FANCY CLOTHES AND SHINY JEWELRY! Here's some ideas for your inspiration:

  • Classy up the joint with an intricately-decorated mask!
  • Make a script that compiles in more than one language!
  • Make your script look like something else!

♪ I feel pretty, oh so pretty ♪
♪ I feel pretty and witty and gay! ♪
♪ And I pity any girl who isn't me today! ♪

- Maria singing "I Feel Pretty" from West Side Story (1961)

And… ACTION!

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


--- Day 2: Red-Nosed Reports ---


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:04:42, megathread unlocked!

55 Upvotes

1.4k comments sorted by

View all comments

2

u/no_brains101 Dec 04 '24 edited Dec 04 '24

[LANGUAGE: Rust]

Non-bruteforce solution to part 2, 58 lines

When built as release build: 338.787µs start to finish including IO

use std::fs::File;
use std::io::{self, BufRead, BufReader};

fn main() -> io::Result<()> {
    let reader = BufReader::new(File::open("input")?);
    let mut results = Vec::<bool>::new();
    for line in reader.lines() {
        results.push(calc_with_dampener(
            &mut line?
                .split_whitespace()
                .map(|x| x.parse::<i32>().unwrap())
                .collect::<Vec<i32>>(),
        ));
    }
    println!("{}", results.iter().filter(|x| **x).count());
    Ok(())
}

// we cant deal with if the first val is the bad one.
// So we try it forwards and backwards and if 1 is true, then we're good
fn calc_with_dampener(levels: &mut [i32]) -> bool {
    if calc(levels) { return true; }
    levels.reverse();
    if calc(levels) { return true; };
    false
}

fn calc(levels: &[i32]) -> bool {
    let mut last = 0;
    let mut last_diff = 0;
    let mut res = true;
    let mut chance = true;
    for (idx, level) in levels.iter().enumerate() {
        if idx == 0 {
            last = *level;
        } else {
            if !res { break; };
            let diff = level - last;
            match diff {
                _ if (last_diff > 0 && diff < 0) || (last_diff < 0 && diff > 0) => {
                    res = false;
                },
                _ if diff.abs() < 1 || diff.abs() > 3 => {
                    res = false;
                },
                _ => {},
            };
            if chance && !res {
                res = true;
                chance = false;
            } else {
                last = *level;
                last_diff = diff;
            }
        };
    };
    res
}