From a9b64a0cfb32ef0f0a08b3444293f9039823217d Mon Sep 17 00:00:00 2001 From: limb Date: Fri, 10 Dec 2021 16:36:57 +0000 Subject: [PATCH] Day 3 Part 2 WIP --- day3/part2/main.cpp | 126 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 105 insertions(+), 21 deletions(-) diff --git a/day3/part2/main.cpp b/day3/part2/main.cpp index 95f1937..c7ba340 100644 --- a/day3/part2/main.cpp +++ b/day3/part2/main.cpp @@ -1,43 +1,127 @@ #include #include #include +#include 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> inputBits; ifstream filein("input"); - vector inputCommands; for(string line; getline(filein, line);) { - int space = line.find(" "); - inputCommands.push_back(Command {line.substr(0, space), stoi(line.substr(space+1))}); + vector 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(Command cmd : inputCommands) { - if(cmd.direction == "forward") { - x += cmd.distance; - y += cmd.distance * aim; + vector> oxygen = inputBits; + + int x, greater = 0; + 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 z) { + if (z[x] != matcher) { + return true; + } + return false; + } + ), oxygen.end() + ); + + x++; + greater = 0; + } + + vector> 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 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; } \ No newline at end of file