adventofcode2021/day3/part1/main.cpp
2021-12-03 20:17:53 +00:00

50 lines
1.0 KiB
C++

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
cout << "Advent of Code 2021 - Day 3 Part 1" << endl;
unsigned int gamma = 0;
unsigned int epsilon = 0;
vector<vector<char>> inputBits;
ifstream filein("input");
for(string line; getline(filein, line);) {
vector<char> bits;
for(int i = 0; i < 12; i++) {
if (line[i] == '1') {
bits.push_back(1);
}
else {
bits.push_back(0);
}
}
inputBits.push_back(bits);
}
for(int x = 0; x < 12; x++) {
int greater = 0;
for(int i = 0; i < inputBits.size(); i++) {
if(inputBits[i][x] == 1) {
greater++;
}
}
if(greater > (inputBits.size() / 2)) {
gamma |= 1UL << 11 - x;
} else {
epsilon |= 1UL << 11 - x;
}
}
cout << gamma << endl;
cout << epsilon << endl;
cout << gamma * epsilon << endl;
return 0;
}