--- Day 3: Rucksack ---

December 3

Listening to: Relaxing SNES music


Hahaha, whoops, I totally spaced on this and started like... almost four hours late. Hopefully I can finish this quickly.


A lot of words and confusion for a simple thing - each line has one matching character in the first and second half of the line. Find it.

My thinking is simply create a set with the first half of the line, then search for each character within that set, and when you get the offending character, that's when you do... the weird addition thing, which is basically subtraction of ASCII codes

set<char> comp1;
int size = line.length();

for (int i = 0; i < size/2; i++) comp1.insert(line[i]);
for (int j = size/2; j < size; j++) {
    if (comp1.find(line[j]) != comp1.end()) {
        sum += getPriority(line[j]);
        break;
    }
}

This one was pretty easy. Instead of looking in one set generated from half of a line, look in two set from whole lines. Generate set 1 and set 2 from the first two lines in a grouping, then check against it using the third line.

set<char> bag1, bag2;

getline(input, line);
for (int i = 0; i < line.length(); i++) bag1.insert(line[i]);

getline(input, line);        
for (int i = 0; i < line.length(); i++) bag2.insert(line[i]);

getline(input, line);
for (int i = 0; i < line.length(); i++) {
    if (bag1.find(line[i]) != bag1.end() && bag2.find(line[i]) != bag2.end()) {
        sum += getPriority(line[i]);
        break;
    }
}

Not too difficult. Then again, it's still just the first few days.


As an aside, I got the right answer the first time I tried submitting it, but mistyped it when inserting it. Kind of annoying that there's an immediate rate limit on giving answers, but it makes sense. I just wish there was more of a penalty system. No rate limiting until 3 wrong answers, or something like that?


Time taken: 15 minutes

My solutions for today's puzzles