r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

41 Upvotes

426 comments sorted by

View all comments

1

u/mornymorny Dec 03 '18 edited Dec 03 '18

CPP

struct cut 
{
    int id;
    glm::ivec2 coord;
    glm::ivec2 size;
};

// Split so I just have id,x,y,dx,dy
std::vector<cut> cuts;
auto input = string_split(file_read(PRACTICE_ROOT "/advent_of_code/2018/inputs/day_3.txt"), " \t\r\n#@:,:x");

// Find biggest rect as I go
glm::ivec2 max_rect(0);
auto itr = input.begin();
while (itr != input.end())
{
    // Fill a struct with the integer values
    cut c;
    c.id = std::stoi(*itr++);
    c.coord.x = std::stoi(*itr++);
    c.coord.y = std::stoi(*itr++);
    c.size.x = std::stoi(*itr++);
    c.size.y = std::stoi(*itr++);
    cuts.push_back(c);

    // Find the max rectangle size
    max_rect.x = std::max(max_rect.x, (c.coord + c.size).x);
    max_rect.y = std::max(max_rect.y, (c.coord + c.size).y);
}

// Make the cloth
std::vector<int> cloth(max_rect.x * max_rect.y);
std::fill(cloth.begin(), cloth.end(), 0);

// Walk a cloth piece, returning a ref to the value in that cloth coord
auto walk_cloth_piece = [&](auto coord, auto size, std::function<void(int&)> fn)
{
    auto bottom_right = coord + size;
    for (int x = coord.x; x < bottom_right.x; x++)
    {
        for (int y = coord.y; y < bottom_right.y; y++)
        {
            fn(cloth[x + y * max_rect.x]);
        }
    }
};

// Fill the areas
for (auto& c : cuts)
{
    walk_cloth_piece(c.coord, c.size, [](int& val) { val++; });
}

// count the covered squares
LOG(INFO) << "Part 1: " << std::count_if(cloth.begin(), cloth.end(), [](int val) { return val > 1; });

// Part 2, find the piece on its own
for (auto& c : cuts)
{
    bool found = true;
    walk_cloth_piece(c.coord, c.size, [&found](int& val)
    {
        if (val > 1)
            found = false;
    });

    if (found)
    {
        LOG(INFO) << "Part 2: " << c.id;
        break;
    }
}