advent of code day 3: i might be r-slurred edition

idk how to do multi line spoilers lol



letters = ['','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

total_score = 0

team = []

for line in open("input.txt"):
    team.append(line.strip())

for i in range(2, len(team), 3):
    for letter in team[i]:
        if letter in team[i-2] and letter in team[i-1]:
            total_score += letters.index(letter)
            break

print(total_score)

some real caveman shit but it works

80
Jump in the discussion.

No email address required.

c++ Part one


int charToPriority(char c)
{
    if (c >= 'a' && c <= 'z') return c - 'a' + 1;
    if (c >= 'A' && c <= 'Z') return c - 'A' + 27;
}

char priorityToChar(int c) {
    if (c < 27) return c + 'a' - 1;
    return c + 'A' - 27;
}

int partOne()
{
    std::cout << "Day 3" << '\n';

    std::ifstream file("input.txt");
    std::string line;

    int score{ 0 };


    while (std::getline(file, line)) {
        std::unordered_map<char int=""> left;
        int n = line.size();
        for (int i{ 0 }; i < n / 2; i++) {
            if (!left[line[i]]) left[line[i]] = 1;
        }
        for (int j = n / 2; j < n; j++) {
            if (left[line[j]] > 0) {
                score += charToPriority(line[j]);
                left[line[j]]--;
            }
        }
    }

    file.close();
    
    return score;
}

</char>

Part two




int partTwo()
{
    std::cout << "Day 3 Part Two" << '\n';

    std::ifstream file("input.txt");
    std::string elf1;
    std::string elf2;
    std::string elf3;

    int score{ 0 };

    while (std::getline(file, elf1)std::getline(file, elf3)) {
        std::vector<int> storage(52);

        for (char c : elf1) storage[charToPriority(c) - 1] |= 0b001;
        for (char c : elf2) storage[charToPriority(c) - 1] |= 0b010;
        for (char c : elf3) {
            storage[charToPriority(c) - 1] |= 0b100;
            if (storage[charToPriority(c) - 1] == 0b111) {
                score += charToPriority(c);
                break;
            }
        }
    }

    file.close();

    return score;
}
</int>

Maybe i should just have just taken the o(n^2) pill and do it with 'in'.

Jump in the discussion.

No email address required.

you're fricking bananas if you think I'm reading all that, take my downmarsey and shut up idiot

Jump in the discussion.

No email address required.

congrats on using a sane language that dosent store everything in unicode by default :marseycry:

Jump in the discussion.

No email address required.

Dont initialize your integers like that, it makes you look like a total fu cking tool. Also, use size_t and ++i in your for loop.

Jump in the discussion.

No email address required.

>don't initalise your integers as reccomended by C++22

you're the reason c++ has namespaces

Jump in the discussion.

No email address required.

C++22

Dude, C++ is on a 3-year cycle, C++17, C++20, C++23. SMH my head plebs pretending to know shit.

Primitive initialization has different behaviors than classes, so there's no reason you need to treat them the same way, even stylistically.

Also, the bigger issue is that awful noob-like for loop that borders on being wrong, not just stylistic differences.

Jump in the discussion.

No email address required.

Amazing parsing of code

![](/images/16700643198100007.webp)

![](/images/16700643556568546.webp)

Jump in the discussion.

No email address required.

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