Add 2021 day 3 puzzles

This commit is contained in:
2021-12-03 12:22:03 +00:00
parent da8f874574
commit b43c4a617e
2 changed files with 125 additions and 0 deletions

44
2021/puzzle-03-01.cc Normal file
View File

@@ -0,0 +1,44 @@
//
// Created by Matthew Gretton-Dann on 03/12/2021.
//
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
auto main() -> int
{
std::vector<unsigned> bit_counts;
std::string line;
unsigned line_count{0};
while (std::getline(std::cin, line)) {
++line_count;
if (bit_counts.empty()) {
bit_counts.resize(line.size(), 0);
}
assert(bit_counts.size() == line.size());
for (std::size_t i{0}; i < line.size(); ++i) {
if (line[i] == '1') {
bit_counts[i]++;
}
}
}
std::uint64_t gamma_rate{0};
std::uint64_t epsilon_rate{0};
for (auto count : bit_counts) {
gamma_rate <<= 1;
epsilon_rate <<= 1;
if (count > line_count / 2) {
gamma_rate |= 1;
}
else {
epsilon_rate |= 1;
}
}
std::cout << "Gamma rate * epsilon rate = " << gamma_rate << " * " << epsilon_rate << " = "
<< gamma_rate * epsilon_rate << '\n';
return 0;
}