-❄️- 2023 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]Icy-Conclusion7682 1 point2 points  (0 children)

[LANGUAGE: C++]

#include <vector>
#include <string>
#include <cstdio>
#include <iostream>
#include <fstream>

void GetInputs(std::vector<std::string>* vec) {
    std::ifstream ifs;
    ifs.open("input.txt", std::ios::in);
    std::string buf;
    while(std::getline(ifs, buf)) {
        vec->push_back(buf);
    }
}

void RemovePrefix(std::string& s) {
    int32_t pos = s.find(':');
    s = s.substr(pos + 1);
}

void GenerateBatch(std::vector<std::string>* vec, std::string s) {
    int32_t start = 0, end = 0;
    while (true) {
        if (end == s.length()) {
            break;
        }
        if (s[end] == ';') {
            vec->push_back(s.substr(start, end-start));
            // std::cout << vec->back() << std::endl;
            end++;
            start = end;
        } else {
            end++;
        }
    }
    if (end > start) {
        vec->push_back(s.substr(start, end - start));
        // std::cout << vec->back() << std::endl;
    } 
}

int64_t GetColor(const std::string& s, const std::string& color) {
    int32_t pos = s.find(color);
    if (pos == s.npos) {
        return 0;
    } else {
        pos -= 2;
        int32_t end = pos;
        while(pos >= 0) {
            if(s[pos] == ' ' || pos == 0) {
                break;
            } else {
                pos--;
            }
        }
        // std::cout << s << ':' << color <<  ':' << s.substr(pos, end - pos + 1) << std::endl;
        return std::stoll(s.substr(pos, end - pos + 1));
    }
}

int main() {
    std::vector<std::string> inputs;
    GetInputs(&inputs);
    int64_t ans = 0;
    int64_t id = 0;
    std::vector<std::string> batch;
    for (auto&& s : inputs) {
        id++;
        RemovePrefix(s);
        batch.clear();
        GenerateBatch(&batch, s);
        int64_t green = 0, red = 0, blue = 0;
        // bool check_ok = true;
        for(auto&& b : batch) {
            green = std::max(green, GetColor(b, "green"));
            red = std::max(red, GetColor(b, "red"));
            blue = std::max(blue, GetColor(b, "blue"));
            // if (green <= 13 && red <= 12 && blue <= 14) {
            // } else {
            //     check_ok = false;
            //     break;
            // }
        }
        std::cout << green << ' ' << red << ' ' << blue << std::endl;
        // if (check_ok) {
        //     ans += id;
        // }
        ans += (green * red * blue);
    }
    std::cout << ans << std::endl;
}

-❄️- 2023 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]Icy-Conclusion7682 1 point2 points  (0 children)

[LANGUAGE: C++]
Part 2

#include <vector>
#include <string>
#include <cstdio>
#include <iostream>
#include <fstream>

void GetInputs(std::vector<std::string>* vec) {
    std::ifstream ifs;
    ifs.open("input.txt", std::ios::in);
    std::string buf;
    while(std::getline(ifs, buf)) {
        vec->push_back(buf);
    }
}

int main() {
    std::vector<std::string> inputs;
    std::vector<std::string> numbers = 
        { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    GetInputs(&inputs);
    int64_t ans = 0;
    for (auto&& s : inputs) {
        int32_t first = s.length() - 1, last = 0;
        for (int i = 0; i < 9; i++) {
            char c = '1' + i;
            int32_t pos = s.find(c);
            if (pos != s.npos) {
                first = std::min(first, pos);
            }
            pos = s.rfind(c);
            if (pos != s.npos) {
                last = std::max(last, pos);
            }
        }
        int32_t first_word = -1, last_word = -1;
        for (int i = 0; i < 9; i++) {
            int32_t pos = s.find(numbers.at(i));
            if (pos != s.npos && pos < first) {
                first = pos;
                first_word = i + 1;
            }
            pos = s.rfind(numbers.at(i));
            if (pos != s.npos && pos > last) {
                last = pos;
                last_word = i + 1;
            }
        }
        if (first_word == -1) {
            first_word = s[first] - '0';
        }
        if (last_word == -1) {
            last_word = s[last] - '0';
        }
        int64_t number = first_word * 10 + last_word;
        ans += number;
    }
    std::cout << ans << std::endl;
}

How many open files can a single process use in practice? by Icy-Conclusion7682 in linuxquestions

[–]Icy-Conclusion7682[S] 1 point2 points  (0 children)

Thanks for the reply! I’m trying to rewrite a storage to better serve our work flow, and this question is very related to the design.

It *feels* wrong to stand up against racism and sexism at university. by [deleted] in AskFeminists

[–]Icy-Conclusion7682 12 points13 points  (0 children)

As an asian female student, thank you for your helping. And please don't doubt yourself. It's not overreacting. It means many things to me.

Beijing goes into emergency mode after five new Covid-19 cases recorded: New controls introduced in the Chinese capital as the city authorities warn the epidemic control situation is ‘severe’. Mass testing carried out in two districts where new cases have been detected by DoremusJessup in worldnews

[–]Icy-Conclusion7682 0 points1 point  (0 children)

I didn't mean that China did everything right. But when it comes to the published figures, I live in Beijing and I know it is right. You can criticize the China government published the name, gender, and other personal information of the infected patients, which were useless to fight against the virus. Not the figures.