Skip to content

Instantly share code, notes, and snippets.

@orendon
Created December 6, 2020 17:54
Show Gist options
  • Save orendon/4bb8cdf6d38089cf2b59a4aa3c4b1334 to your computer and use it in GitHub Desktop.
Save orendon/4bb8cdf6d38089cf2b59a4aa3c4b1334 to your computer and use it in GitHub Desktop.
Advent of Code 2020 - Day 6 Custom Customs
#include <iostream>
#include <string>
#include <set>
using namespace std;
#define endl "\n"
int main() {
string line;
int part1_answer=0, part2_answer=0;
int group_OR=0, group_AND, person_OR;
group_AND = ~group_OR; // AND reset, set all bits
while (getline(cin, line)) {
if (line == "") { // next group
part1_answer += __builtin_popcount(group_OR);
part2_answer += __builtin_popcount(group_AND);
group_OR = 0;
group_AND = ~group_OR;
} else {
person_OR = 0;
for(char &c : line) {
person_OR |= 1 << (c-96);
group_OR |= person_OR;
}
group_AND &= person_OR;
}
}
part1_answer += __builtin_popcount(group_OR);
part2_answer += __builtin_popcount(group_AND);
cout << "Part 1: " << part1_answer << endl;
cout << "Part 2: " << part2_answer << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment