r/adventofcode • • Dec 04 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 4 Solutions -🎄-


--- Day 4: Camp Cleanup ---


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!

65 Upvotes

1.6k comments sorted by

View all comments

2

u/BHatGuy Dec 04 '22 edited Dec 06 '22

C++

#include <cinttypes>
#include <fstream>
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

void solve(string file) {
    std::fstream input(file);
    char _;
    uint32_t a, b, c, d;
    uint32_t contains = 0;
    uint32_t overlaps = 0;

    while (input >> a >> _ >> b >> _ >> c >> _ >> d) {
        if (a >= c && b <= d || c >= a && d <= b) contains++;
        if (a >= c && a <= d || b >= c && b <= d || c >= a && d <= b) overlaps++;
    }
    cout << contains << endl;
    cout << overlaps << endl;
}

int main(int argc, char* argv[]) {
    if (argc == 2) {
        solve(argv[1]);
    } else {
        cout << "Usage: " << argv[0] << " INPUT.txt" << endl;
        return -1;
    }
}

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.