Day 3 WIP
This commit is contained in:
57
day3/part1/main.cpp
Normal file
57
day3/part1/main.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
cout << "Advent of Code 2021 - Day 3 Part 1" << endl;
|
||||
|
||||
unsigned char gamma[12] = {0};
|
||||
unsigned char epsilon[12] = {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[x] = 1;
|
||||
epsilon[x] = 0;
|
||||
} else {
|
||||
gamma[x] = 0;
|
||||
epsilon[x] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
int asd = 0;
|
||||
|
||||
for(int x = 0; x < 12; x++) {
|
||||
asd = asd << (int)gamma[x];
|
||||
}
|
||||
cout << asd << endl;
|
||||
|
||||
cout << epsilon << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user