From b43c4a617e483797224156926b19912c1d955ffe Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Fri, 3 Dec 2021 12:22:03 +0000 Subject: [PATCH] Add 2021 day 3 puzzles --- 2021/puzzle-03-01.cc | 44 ++++++++++++++++++++++++ 2021/puzzle-03-02.cc | 81 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 2021/puzzle-03-01.cc create mode 100644 2021/puzzle-03-02.cc diff --git a/2021/puzzle-03-01.cc b/2021/puzzle-03-01.cc new file mode 100644 index 0000000..847032d --- /dev/null +++ b/2021/puzzle-03-01.cc @@ -0,0 +1,44 @@ +// +// Created by Matthew Gretton-Dann on 03/12/2021. +// + +#include +#include +#include +#include + +auto main() -> int +{ + std::vector 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; +} \ No newline at end of file diff --git a/2021/puzzle-03-02.cc b/2021/puzzle-03-02.cc new file mode 100644 index 0000000..bc91316 --- /dev/null +++ b/2021/puzzle-03-02.cc @@ -0,0 +1,81 @@ +// +// Created by Matthew Gretton-Dann on 03/12/2021. +// + +#include +#include +#include +#include +#include + +auto to_num(std::string const& s) -> std::uint64_t +{ + std::uint64_t result{0}; + for (auto c : s) { + result <<= 1; + result |= (c == '1' ? 1 : 0); + } + return result; +} + +auto find_oxygen_rating(std::vector const& lines) -> std::string +{ + std::vector data{lines}; + assert(!data.empty()); + std::size_t len{data[1].size()}; + for (std::size_t i{0}; i < len; ++i) { + auto one_bits{std::accumulate( + data.begin(), data.end(), std::uint64_t{0}, + [i](std::uint64_t a, std::string const& s) { return a + (s[i] == '1' ? 1 : 0); })}; + char keep{(one_bits * 2 >= data.size()) ? '1' : '0'}; + std::cout << "Size of data = " << data.size() << " Number of one bits = " << one_bits + << " keeping " << keep << '\n'; + auto it{std::remove_if(data.begin(), data.end(), + [keep, i](std::string const& s) { return s[i] != keep; })}; + data.erase(it, data.end()); + if (data.size() <= 1) { + break; + } + } + assert(data.size() == 1); + return data[0]; +} + +auto find_co2_rating(std::vector const& lines) -> std::string +{ + std::vector data{lines}; + assert(!data.empty()); + std::size_t len{data[1].size()}; + for (std::size_t i{0}; i < len; ++i) { + auto one_bits{std::accumulate( + data.begin(), data.end(), std::uint64_t{0}, + [i](std::uint64_t a, std::string const& s) { return a + (s[i] == '1' ? 1 : 0); })}; + char keep{(one_bits * 2 >= data.size()) ? '0' : '1'}; + std::cout << "Size of data = " << data.size() << " Number of one bits = " << one_bits + << " keeping " << keep << '\n'; + auto it{std::remove_if(data.begin(), data.end(), + [keep, i](std::string const& s) { return s[i] != keep; })}; + data.erase(it, data.end()); + if (data.size() <= 1) { + break; + } + } + assert(data.size() == 1); + return data[0]; +} + +auto main() -> int +{ + std::vector lines; + std::string line; + while (std::getline(std::cin, line)) { + lines.push_back(line); + } + + auto oxygen_rating{to_num(find_oxygen_rating(lines))}; + auto co2_rating{to_num(find_co2_rating(lines))}; + + std::cout << "Oxygen generator rating * CO2 scrubber rating = " << oxygen_rating << " * " + << co2_rating << " = " << oxygen_rating * co2_rating << '\n'; + return 0; +} \ No newline at end of file