Day 3 Part 2 WIP

This commit is contained in:
limb 2021-12-10 16:36:57 +00:00
parent 5533ef2b25
commit a9b64a0cfb

View File

@ -1,43 +1,127 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <algorithm>
using namespace std; using namespace std;
struct Command {
string direction;
int distance;
};
int main() { 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"); ifstream filein("input");
vector<Command> inputCommands;
for(string line; getline(filein, line);) { for(string line; getline(filein, line);) {
int space = line.find(" "); vector<char> bits;
inputCommands.push_back(Command {line.substr(0, space), stoi(line.substr(space+1))}); 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(Command cmd : inputCommands) { vector<vector<char>> oxygen = inputBits;
if(cmd.direction == "forward") {
x += cmd.distance; int x, greater = 0;
y += cmd.distance * aim; while (oxygen.size() != 1) {
for(int i = 0; i < oxygen.size(); i++) {
if(oxygen[i][x] == 1) {
greater++;
}
} }
else if (cmd.direction == "down") {
aim += cmd.distance; char matcher;
if(greater > (oxygen.size() / 2)) {
matcher = 1;
} else {
matcher = 0;
} }
else if(cmd.direction == "up") {
aim -= cmd.distance; 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++;
}
} }
else {
cout << "Uh I shouldn't get here in the for cmd loop" << endl; 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 << x * y << endl; cout << oxygen_final << endl << co2_final << endl;
cout << oxygen_final * co2_final << endl;
return 0; return 0;
} }