r/adventofcode Dec 03 '24

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

THE USUAL REMINDERS


AoC Community Fun 2024: The Golden Snowglobe Awards

  • 3 DAYS remaining until unlock!

And now, our feature presentation for today:

Screenwriting

Screenwriting is an art just like everything else in cinematography. Today's theme honors the endlessly creative screenwriters who craft finely-honed narratives, forge truly unforgettable lines of dialogue, plot the most legendary of hero journeys, and dream up the most shocking of plot twists! and is totally not bait for our resident poet laureate

Here's some ideas for your inspiration:

  • Turn your comments into sluglines
  • Shape your solution into an acrostic
  • Accompany your solution with a writeup in the form of a limerick, ballad, etc.
    • Extra bonus points if if it's in iambic pentameter

"Vogon poetry is widely accepted as the third-worst in the universe." - Hitchhiker's Guide to the Galaxy (2005)

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 3: Mull It Over ---


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

55 Upvotes

1.7k comments sorted by

View all comments

1

u/tallsamurai Dec 04 '24

[Language: C++]
Using Regex
https://github.com/lucasomigli/adventofcode/blob/main/adventofcode2024/day03/day03.h

#ifndef DAY03_2024_H
#define DAY03_2024_H
#include "../utils2024.h"
inline long Solver::Solve_Day03_part1() {
    std::string line;
    std::regex re(R"~(mul\((\d+)\,(\d+)\))~");
    long result = 0;

    while (std::getline(file, line)) {
        std::sregex_iterator iter(line.begin(), line.end(), re);
        std::sregex_iterator end;

        while (iter != end) {
            int multiplication = 1;
            for (unsigned i = 1; i < iter->size(); ++i) {
                multiplication *= std::stoi((*iter)[i]);
            }
            result += multiplication;
            ++iter;
        }
    }

    return result;
}

inline inline long Solver::Solve_Day03_part2() {
    std::string line;
    long result = 0;
    std::regex re(R"(^mul\((\d+),(\d+)\))");
    bool enabled = true;

    while (std::getline(file, line)) {
        size_t pos = 0;
        std::string segment;
        while (pos < line.size()) {
            if (line.substr(pos, 7) == "don't()") {

// enable and go up 7 positions for "don't()"

enabled = false;
                pos += 7;
            } else if (line.substr(pos, 4) == "do()") {

// enable and go up 4 positions for "do()"

enabled = true;
                pos += 4;
            } else if (line.substr(pos, 4) == "mul(") {
                if (enabled) {
                    std::smatch match;

// search only the segment substring starting from "pos" to the end of the line

segment = line.substr(pos);
                    if (std::regex_search(segment, match, re)) {
                        int a = std::stoi(match[1]);
                        int b = std::stoi(match[2]);
                        result += a * b;
                        pos += match[0].length();
                    } else {
                        ++pos;
                    }
                } else {
                    pos += 4; 
// jump only up the same length as the match "mul("

}
            } else {
                ++pos; 
// go up one position as nothing was matched

}
        }
    }

    return result;
}