Unable to load image

The luckiest of days, Day 13!!

Let's fricking gooo!

17
Jump in the discussion.

No email address required.

It is a bit of a mess and i would prefer to do it without pointers(or at least use smart_pointers, not have dangle like absolute every pointer, but if a add a destructor everything breaks), but for some reason i couldnt get my parser to work without pointers. Overall, pretty fucking horrible in c++.


#include <iostream>
#include <vector>
#include <locale>
#include <string>
#include <stack>
#include <fstream>
#include <algorithm>
#include <iterator>

class NumberOrList;
int cmp(const NumberOrList* left, const NumberOrList* right);

class NumberOrList {
public:
    int number{ -1 };
    std::vector<NumberOrList*> list;

    NumberOrList(int a) {
        number = a;
    }

    NumberOrList() {
        number = -1;
    };

    bool isNumber() const {
        return number >= 0;
    }

    bool isList() const {
        return !isNumber();
    }

    bool operator<(const NumberOrList n) const {
        return cmp(this, &n) < 0;
    }

    bool operator==(const NumberOrList n) const {
        if (number != n.number) return false;
        if (list.size() != n.list.size()) return false;
        for (size_t i{ 0 }; i < list.size(); i++) {
            if (!(*list[i] == *n.list[i])) return false;
        }
        return true;
    }
};

int cmp(const NumberOrList* left, const NumberOrList* right) {
    if (left->isNumber() && right->isNumber()) return left->number - right->number;
    if (left->isList() && right->isList()) {
        auto left_it = left->list.begin();
        auto right_it = right->list.begin();
        for (; left_it != left->list.end() && right_it != right->list.end(); ++left_it, ++right_it) {
            int r = cmp(*left_it, *right_it);
            if (r != 0) return r;
        }
        return left->list.size() - right->list.size();
    }
    if (right->isList()) {
        auto temp = new NumberOrList();
        temp->list.push_back(new NumberOrList(left->number));
        int r = cmp(temp, right);
        delete temp->list.back();
        delete temp;
        return r;
    }
    else if (left->isList()) {
        auto temp = new NumberOrList();
        temp->list.push_back(new NumberOrList(right->number));
        int r = cmp(left, temp);
        delete temp->list.back();
        delete temp;
        return r;
    }
}

NumberOrList create_nl(std::string a) {
    std::string numbuf{ "" };

    std::stack<NumberOrList*> stack;
    stack.push(new NumberOrList);


    for (auto c : a) {
        if (std::isdigit(c)) numbuf += c;
        else {
            if (numbuf.size() > 0) stack.top()->list.push_back(new NumberOrList(std::stoi(numbuf)));
            numbuf = "";
        }
        if (c == '[') {
            auto new_nl = new NumberOrList();
            stack.top()->list.push_back(new_nl);
            stack.push(new_nl);
        }
        if (c == ']') {
            stack.pop();
        }
    }
    auto out = *(stack.top()->list[0]);
    stack.top()->list = {};
    delete stack.top();
    return out;
}

int main()
{
    std::ifstream file("input.txt");
    std::string line;
    std::vector<NumberOrList> lines;
    int i{ 1 };
    int result{ 0 };
    std::cout << "START\n";

    while (std::getline(file, line)) {
        auto line_1 = create_nl(line);
        std::getline(file, line);
        auto line_2 = create_nl(line);
        std::getline(file, line);
        lines.push_back(line_1);
        lines.push_back(line_2);
        if (line_1 < line_2) result += i;
        ++i;
    }

    NumberOrList two;
    two.list.push_back(new NumberOrList(2));
    lines.push_back(two);
    NumberOrList six;
    six.list.push_back(new NumberOrList(6));
    lines.push_back(six);

    std::sort(lines.begin(), lines.end());

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

    int partwo{ 1 };

    for (size_t i{ 0 }; i < lines.size(); ++i) {
        if (lines[i] == two) {
            partwo *= i + 1;
            std::cout << "TWO: " << i << '\n';
        }
        if (lines[i] == six) {
            partwo *= i + 1;
            std::cout << "SIX: " << i << '\n';
        }
    }
    std::cout << "Part Two: " << partwo << '\n';
}


Jump in the discussion.

No email address required.

:marseyneko: autismcatty says: „ your code stinks!“

Jump in the discussion.

No email address required.

it does, but i literally just updated it to not use Pointers. Should be less awful

https://rdrama.net/h/programming/post/130976/the-luckiest-of-days-day-13/3235625?context=8#context

Jump in the discussion.

No email address required.

fixed it, all i really needed for it to work was a copy constructor.


#include <iostream>
#include <vector>
#include <locale>
#include <string>
#include <stack>
#include <fstream>
#include <algorithm>
#include <iterator>

class NumberOrList {
public:
    int number{ -1 };
    std::vector<NumberOrList> list;

    NumberOrList(int a) {
        number = a;
    }

    NumberOrList() {
        number = -1;
    };

    NumberOrList(const NumberOrList& nl) {
        number = nl.number;
        for (const auto& a : nl.list) list.push_back(a);
    }

    NumberOrList(std::string str) {
        for (size_t i{ 0 }; i < str.size(); ++i) {
            if (str[i] == '[') {
                std::string buf = "";
                int count_open = 1;
                ++i;
                while (count_open > 0) {
                    if (str[i] == '[') ++count_open;
                    if (str[i] == ']') --count_open;
                    if (count_open == 0) {
                        list.push_back(buf);
                    }
                    else {
                        buf += str[i];
                    }
                    ++i;
                }
            }
            if (std::isdigit(str[i])) {
                std::string buf{ "" };
                while (std::isdigit(str[i])) {
                    buf += str[i];
                    i++;
                }
                list.push_back(std::stoi(buf));
                
            }
        }
    }

    bool isNumber() const {
        return number >= 0;
    }

    bool isList() const {
        return !isNumber();
    }

    bool operator==(const NumberOrList n) const {
        if (number != n.number) return false;
        if (list.size() != n.list.size()) return false;
        for (size_t i{ 0 }; i < list.size(); i++) {
            if (!(list[i] == n.list[i])) return false;
        }
        return true;
    }

    auto operator<=>(const NumberOrList& right) const {
        if (isNumber() && right.isNumber()) return number - right.number;
        else if (isList() && right.isList()) {
            auto l_it = list.begin();
            auto r_it = right.list.begin();
            for (; l_it != list.end() && r_it != right.list.end(); ++l_it, ++r_it) {
                int r = (*l_it)<=>(*r_it);
                if (r != 0) return r;
            }
            return static_cast<int>(list.size() - right.list.size());
        }
        else if (right.isList()) {
            auto temp = NumberOrList();
            temp.list.push_back(NumberOrList(number));
            return temp <=> right;
        }
        else {
            auto temp = NumberOrList();
            temp.list.push_back(NumberOrList(right.number));
            return *this <=> temp;
        }

    }
};

std::ostream& operator<<(std::ostream& os, const NumberOrList& nl) {
    if (nl.isNumber()) os << nl.number;
    else {
        os << "[";
        std::string pre = "";
        for (const auto& a : nl.list) {
            os << pre << a;
            pre = ",";
        }
        os << "]";
    }
    return os;
}

int main() {
    std::ifstream file("input.txt");
    std::string line;
    std::vector<NumberOrList> lines;
    int i{ 1 };
    int result{ 0 };
    std::cout << "START\n";

    while (std::getline(file, line)) {
        auto line_1 = NumberOrList(line);
        std::getline(file, line);
        auto line_2 = NumberOrList(line);
        std::getline(file, line);

        lines.emplace_back(line_1);
        lines.emplace_back(line_2);

        if (line_1 < line_2) {
            result += i;
        }
        i++;
    }

    auto two = NumberOrList("[[2]]");
    auto six = NumberOrList("[[6]]");
    lines.emplace_back(two);
    lines.emplace_back(six);
    std::sort(lines.begin(), lines.end());

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

    auto location_two = std::lower_bound(lines.begin(), lines.end(), two);
    auto location_six = std::lower_bound(location_two, lines.end(), six);

    int partwo = (1 + std::distance(lines.begin(), location_two)) * (1 + std::distance(lines.begin(), location_six));

    std::cout << "Part Two: " << partwo << '\n';
}

Jump in the discussion.

No email address required.

god i love how compact ts is to cpp

Jump in the discussion.

No email address required.

OUT!

Jump in the discussion.

No email address required.

You had a chance to not be completely worthless, but it looks like you threw it away. At least you're consistent.

Jump in the discussion.

No email address required.



Now playing: Main Menu (DK64).mp3

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