--- Day 8: Segmented ---

December 8

Listening to: lofi hip hop beats music to study/relax to


Okay, I can tell this one is going to be WILD. A malfunctioning 7-segment display. At least part 1 is easy.

if (line.size() == 2 || // 1
    line.size() == 4 || // 4
    line.size() == 3 || // 7
    line.size() == 7) { // 8
    numFound++;
}

Unsurprisingly, part 2 is extremely difficult. Or at least tricky. I don't really see any shortcuts to solving it, but I can use the help they gave from part one.

I've noticed that of the remaining 6, they either have 5 or 6 segments, 3 in each camp. I only need to distinguish the 069 from each other, and 235 from each other.

By comparing their segments to known segments, I can pretty easily determine it. For example, 3 contains all of the segments that 7 contains, while 2 and 5 each have one segment missing compared to 7. I just do that down the line, and it's process of elimination. I just needed a helper function to do all of the comparisons for me, and tell me if it's what I'm expecting.

bool matchCount (string unknown, string known, int goal) {
    int match = 0;
    for (int i = 0; i < known.size(); i++)
        if (unknown.find(known[i]) != string::npos)
            match++;

    return match == goal;
}

After that, I realized I just need to string compare between the data segments and what I just sused out. Easiest way was to sort all of the strings as I read them in, then just do a direct comparison.

// 6 - contains 1 seg of 1
for (i = 0; i < 3; i++) {
    if (matchCount(sixes[i], known[1], 1)) {
        known[6] = sixes[i];
        sixes.erase(sixes.begin() + i);
        break;
    }
}

And after that, it's figuring out the numbers and summing them together. Pretty tough, but I enjoyed it in the end. Just wish I could have come up with something a bit more generalized, but I guess I can only do so much in this case.


My solutions for today's puzzles