Unable to load image

Advent of code day 4 :marseysad:

This is the advent of code day 4 thread for bragging about your solution and bullying people who haven't gotten it yet :marseyill:

35
Jump in the discussion.

No email address required.

c++


struct Elf {
    int min;
    int max;

    bool inline isContainedOrContains(const Elf& b) const {
        return (min >= b.min and max <= b.max) || (b.min >= min && b.max <= max);
    }

    bool inline intersect(const Elf& b) const {
        return (min <= b.max && b.min <= max);
    }
};



std::tuple<int int=""> solution()
{
    std::cout << "Day 4" << '\n';

    std::ifstream file("input.txt");
    std::string line;

    int p1{ 0 };
    int p2{ 0 };

    while (std::getline(file, line)) {

        int a{ 0 };
        int b{ 0 };
        Elf elf1{};
        Elf elf2{};

        std::string buffer;
        for (char c : line) {
            if (c == '-') {
                a = std::stoi(buffer);
                buffer = "";
            }
            else if (c == ',') {
                b = std::stoi(buffer);
                elf1 = { a, b };
                buffer = "";
            }
            else buffer += c;
        }
        elf2 = { a, std::stoi(buffer)};

        if (elf1.isContainedOrContains(elf2)) ++p1;
        if (elf1.intersect(elf2)) ++p2;
    }

    file.close();

    return { p1, p2 };
}
</int>
Jump in the discussion.

No email address required.

Mommy is soooo proud of you, sweaty. Let's put this sperg out up on the fridge with all your other failures.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.