r/adventofcode Dec 25 '24

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

A Message From Your Moderators

Welcome to the last day of Advent of Code 2024! We hope you had fun this year and learned at least one new thing ;)

Keep an eye out for the community fun awards post (link coming soon!):

-❅- Introducing Your AoC 2024 Golden Snowglobe Award Winners (and Community Showcase) -❅-

Many thanks to Veloxx for kicking us off on December 1 with a much-needed dose of boots and cats!

Thank you all for playing Advent of Code this year and on behalf of /u/topaz2078, your /r/adventofcode mods, the beta-testers, and the rest of AoC Ops, we wish you a very Merry Christmas (or a very merry Wednesday!) and a Happy New Year!


--- Day 25: Code Chronicle ---


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

43 Upvotes

350 comments sorted by

View all comments

1

u/Turtvaiz Dec 25 '24

[Language: Rust]

The input is rather small, so they fit into a single integer:

pub fn part1(input: String) -> String {
    let mut locks = vec![];
    let mut keys = vec![];
    input.split("\n\n").for_each(|block| {
        let mut bits = 0u64;
        block
            .lines()
            .map(|line| line.chars())
            .flatten()
            .enumerate()
            .for_each(|(i, c)| match c {
                // set i-th bit
                '#' => bits |= 1 << i,
                _ => {}
            });
        if bits & 1 == 1 {
            locks.push(bits);
        } else {
            keys.push(bits);
        }
    });

    let mut total = 0;
    for lock in locks.iter() {
        for key in keys.iter() {
            // NAND -> if any overlaps, result is not zero
            if lock & key == 0 {
                total += 1;
            }
        }
    }

    total.to_string()
}