Day 3 Part 2 WIP
This commit is contained in:
parent
5533ef2b25
commit
a9b64a0cfb
|
|
@ -1,43 +1,127 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Command {
|
||||
string direction;
|
||||
int distance;
|
||||
};
|
||||
|
||||
int main() {
|
||||
cout << "Advent of Code 2021 - Day 2 Part 2" << endl;
|
||||
cout << "Advent of Code 2021 - Day 3 Part 2" << endl;
|
||||
|
||||
int x, y, aim = 0;
|
||||
vector<vector<char>> inputBits;
|
||||
|
||||
ifstream filein("input");
|
||||
vector<Command> inputCommands;
|
||||
for(string line; getline(filein, line);) {
|
||||
int space = line.find(" ");
|
||||
inputCommands.push_back(Command {line.substr(0, space), stoi(line.substr(space+1))});
|
||||
}
|
||||
|
||||
for(Command cmd : inputCommands) {
|
||||
if(cmd.direction == "forward") {
|
||||
x += cmd.distance;
|
||||
y += cmd.distance * aim;
|
||||
}
|
||||
else if (cmd.direction == "down") {
|
||||
aim += cmd.distance;
|
||||
}
|
||||
else if(cmd.direction == "up") {
|
||||
aim -= cmd.distance;
|
||||
vector<char> bits;
|
||||
for(int i = 0; i < 12; i++) {
|
||||
if (line[i] == '1') {
|
||||
bits.push_back(1);
|
||||
}
|
||||
else {
|
||||
cout << "Uh I shouldn't get here in the for cmd loop" << endl;
|
||||
bits.push_back(0);
|
||||
}
|
||||
}
|
||||
inputBits.push_back(bits);
|
||||
}
|
||||
|
||||
vector<vector<char>> oxygen = inputBits;
|
||||
|
||||
int x, greater = 0;
|
||||
while (oxygen.size() != 1) {
|
||||
for(int i = 0; i < oxygen.size(); i++) {
|
||||
if(oxygen[i][x] == 1) {
|
||||
greater++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << x * y << endl;
|
||||
char matcher;
|
||||
if(greater > (oxygen.size() / 2)) {
|
||||
matcher = 1;
|
||||
} else {
|
||||
matcher = 0;
|
||||
}
|
||||
|
||||
oxygen.erase(
|
||||
remove_if(
|
||||
oxygen.begin(),
|
||||
oxygen.end(),
|
||||
[x, matcher](vector<char> z) {
|
||||
if (z[x] != matcher) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
), oxygen.end()
|
||||
);
|
||||
|
||||
x++;
|
||||
greater = 0;
|
||||
}
|
||||
|
||||
vector<vector<char>> co2 = inputBits;
|
||||
|
||||
x = 0;
|
||||
greater = 0;
|
||||
|
||||
while (co2.size() != 1) {
|
||||
for(int i = 0; i < co2.size(); i++) {
|
||||
if(co2[i][x] == 0) {
|
||||
greater++;
|
||||
}
|
||||
}
|
||||
|
||||
char matcher;
|
||||
if(greater > (co2.size() / 2)) {
|
||||
matcher = 0;
|
||||
} else {
|
||||
matcher = 1;
|
||||
}
|
||||
|
||||
co2.erase(
|
||||
remove_if(
|
||||
co2.begin(),
|
||||
co2.end(),
|
||||
[x, matcher](vector<char> z) {
|
||||
if (z[x] != matcher) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
), co2.end()
|
||||
);
|
||||
|
||||
x++;
|
||||
greater = 0;
|
||||
}
|
||||
|
||||
for (auto c : oxygen) {
|
||||
for (auto z : c) {
|
||||
cout << +z;
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
for (auto c : co2) {
|
||||
for (auto z : c) {
|
||||
cout << +z;
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
unsigned int oxygen_final = 0;
|
||||
unsigned int co2_final = 0;
|
||||
|
||||
for(int x = 0; x < 12; x++) {
|
||||
if(oxygen[0][x] == 1) {
|
||||
oxygen_final |= 1UL << 11 - x;
|
||||
}
|
||||
if(co2[0][x] == 1) {
|
||||
co2_final |= 1UL << 11 - x;
|
||||
}
|
||||
}
|
||||
|
||||
cout << oxygen_final << endl << co2_final << endl;
|
||||
cout << oxygen_final * co2_final << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user