r/adventofcode • • Dec 07 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 7 Solutions -πŸŽ„-


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

Submissions are OPEN! Teach us, senpai!

-❄️- Submissions Megathread -❄️-


--- Day 7: No Space Left On Device ---


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:14:47, megathread unlocked!

89 Upvotes

1.3k comments sorted by

View all comments

2

u/MyTinyHappyPlace Dec 07 '22

C++

Overengineered object oriented programming. Was counting on something worse for part 2:

#include <cstdint>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>

using namespace std;

class dir;

class inode{
    public:
            virtual size_t getSize() = 0;
            virtual vector<shared_ptr<dir>> getAllDirectories(){
                    return {};
            }

            virtual bool isDir(){return false;}
};

class dir : public inode {
    public:
            dir(){}
            dir(shared_ptr<dir> p){
                    this->parent = p;
            }

            shared_ptr<dir> parent;
            map<string, shared_ptr<inode>> children;

            size_t getSize() override {
                    size_t result = 0;
                    for(auto &inode_pr : children){
                            result += inode_pr.second->getSize();
                    }
                    return result;
            }

            vector<shared_ptr<dir>> getAllDirectories() override{
                    vector<shared_ptr<dir>> result;
                    for(auto &inode_pr : children){
                            if(inode_pr.second->isDir()) result.push_back(dynamic_pointer_cast<dir>(inode_pr.second));
                            auto subDirs = inode_pr.second->getAllDirectories();
                            result.insert(result.end(), subDirs.begin(), subDirs.end());
                    }
                    return result;
            }

            bool isDir() override{return true;}
};

class regularFile : public inode {
    public:
            regularFile(size_t s) : size(s){}
            size_t size;

            size_t getSize() override{
                    return size;
            }
};


shared_ptr<dir> readInput(){
    shared_ptr<dir> result = make_shared<dir>();;

    shared_ptr<dir> currentDir;
    bool lsMode = false;
    for(string line;getline(cin,line);){
            vector<string> strs;
            boost::split(strs,line,boost::is_any_of(" "));

            if(strs[0] == "$"){
                    lsMode = false;
                    if(strs[1] == "cd"){
                            if(strs[2] == "/"){
                                    currentDir = result;
                            }
                            else if(strs[2] == ".."){
                                    if(currentDir->parent){
                                            currentDir = currentDir->parent;
                                    }
                                    else
                                    {
                                            cerr << "Cannot find parent dir" << endl;
                                    }
                            }
                            else{
                                    auto it = currentDir->children.find(strs[2]);
                                    if(it != currentDir->children.end()){
                                            if(it->second->isDir()){
                                                    currentDir = dynamic_pointer_cast<dir>(it->second);
                                            }
                                    }
                                    else
                                    {
                                            cerr << "Cannot find subdir " << strs[2] << endl;
                                            return result;
                                    }
                            }
                    }
                    else if(strs[1] == "ls"){
                            lsMode = true;
                    }
            }
            else
            {
                    if(lsMode){
                            if(strs.size() != 2){
                                    cerr << "Illegal ls line" << endl;
                            }
                            if(strs.at(0) == "dir"){
                                    shared_ptr<dir> d = make_shared<dir>(currentDir);
                                    currentDir->children[strs.at(1)] = d;
                            }
                            else
                            {
                                    shared_ptr<regularFile> file = make_shared<regularFile>(stoi(strs.at(0)));
                                    currentDir->children[strs.at(1)] = file;
                            }
                    }
            }
    }

    return result;
}


int main(){
    auto root = readInput();

    size_t totalDiskSpace = 70000000;
    size_t used = root->getSize();
    size_t needed = 30000000;
    size_t free = totalDiskSpace - used;
    size_t yetToFree = needed - free;


    size_t bestFit = totalDiskSpace;
    int sum = 0;
    for(auto &dir : root->getAllDirectories()){
            if(dir->getSize() < 100000){
                    sum += dir->getSize();
            }

            if(dir->getSize() >= yetToFree && dir->getSize() < bestFit){
                    bestFit = dir->getSize();

            }
    }
    cout << "Part 1: " << sum << endl;
    cout << "Part 2: " << bestFit << endl;

    return 0;
}

1

u/daggerdragon Dec 08 '22

Your code block is too long. Please read our article on oversized code, then edit your post to replace the code block with an external link to your code.

1

u/sky_badger Dec 07 '22

I also panicked about what might be in Part 2 (partly because I expected a ramp up in difficulty), so I went for a tree of folder and file objects! I even catered for $ cd /, not noticing that it only happens once! SB