Unable to load image

day 15 thread of aoc i guess lol: diamonds are forever

see leaderboard on geeses post

33
Jump in the discussion.

No email address required.

Just a boring brute force solution, with some logic to skip to the end of the diamond for part two. Times seem fine enough.


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <scn/scn.h>
#include <algorithm>
#include <array>
#include <utility>
#include <chrono>

class Point {
public:
    int x;
    int y;

    Point(int x, int y) {
        this->x = x;
        this->y = y;
    }

    Point() {
        x = 0;
        y = 0;
    }

    int distance(const Point& b) const {
        return std::abs(x - b.x) + std::abs(y - b.y);
    }

    auto operator<=>(const Point&) const = default;

    friend std::ostream& operator<<(std::ostream& os, const Point& p) {
        os << "{x: " << p.x << ", y: " << p.y << "}";
        return os;
    }
};

class Data {
public:
    Point beacon;
    Point sensor;
    int64_t distance;

    Data(Point beacon, Point sensor) {
        this->beacon = beacon;
        this->sensor = sensor;
        distance = beacon.distance(sensor);
    }

    bool point_in_coverage(const Point& b) const {
        if (b.distance(sensor) > distance) return false;
        return true;
    }
};

int main()
{
    std::fstream file("input.txt");
    std::string buf;
    std::vector<Data> data;
    std::vector<Point> beacons;

    while (std::getline(file, buf)) {
        Point beacon;
        Point sensor;

        scn::scan(buf, "Sensor at x = {}, y = {}: closest beacon is at x = {}, y = {}", sensor.x, sensor.y, beacon.x, beacon.y);
        data.push_back({ beacon, sensor });
        beacons.push_back(beacon);
    }

    auto a = std::chrono::high_resolution_clock::now();
    int64_t count = 0;
    for (int i = -10000000 + 10; i < 10000000; i++) {
        Point p{ i, 2000000 };
        bool free = true;
        for (const auto& d : data) {
            if (p != d.beacon && d.point_in_coverage(p)) {
                ++count;
                break;
            }
        }
    }
    auto a_done = std::chrono::high_resolution_clock::now();

    std::cout << "Part One: " << count << '\n';

    // Part Two

    auto b = std::chrono::high_resolution_clock::now();
    std::vector<Point> points;

    Point f_b;

    for (int y = 0; y <= 4'000'000; y++) {
        for (int x = 0; x < 4'000'000; x++) {
            Point p{ x, y };
            bool in = false;
            for (const auto& d : data) {
                if (d.point_in_coverage(p)) {
                    in = true;
                    x = d.sensor.x + (d.distance - std::abs(d.sensor.y - y));
                    break;
                }
            }
            if (!in) {
                f_b = p;
                goto end;
            }
        }
    }
end:
    int64_t result = (int64_t)f_b.x * 4000000 + (int64_t)f_b.y;
    std::cout << "Part Two: " << result << '\n';
    auto b_done = std::chrono::high_resolution_clock::now();


    auto p1 = std::chrono::duration_cast<std::chrono::milliseconds>(a_done - a);
    auto p2 = std::chrono::duration_cast<std::chrono::milliseconds>(b_done - b);

    std::cout << "Time p1: " << p1 << " Time p2: " << p2;
}

![](/images/16711271579454572.webp)

Jump in the discussion.

No email address required.

I don't have enough spoons to read this shit

Jump in the discussion.

No email address required.

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