From 51cec3ea26133867b90a2a855fc0af1ae6e14ae8 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Wed, 1 Dec 2021 20:56:22 +0000 Subject: [PATCH] Add day 1 of 2021's puzzles. --- 2021/puzzle-01-01.cc | 23 +++++++++++++++++++++++ 2021/puzzle-01-02.cc | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 2021/puzzle-01-01.cc create mode 100644 2021/puzzle-01-02.cc diff --git a/2021/puzzle-01-01.cc b/2021/puzzle-01-01.cc new file mode 100644 index 0000000..0bcb7a9 --- /dev/null +++ b/2021/puzzle-01-01.cc @@ -0,0 +1,23 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2021. +// + +#include +#include +#include +#include + +int main(void) { + unsigned long prev{std::numeric_limits::max()}; + unsigned incrs{0}; + std::string line; + while (std::getline(std::cin, line)) { + auto current{std::stoul(line)}; + if (current > prev) { + ++incrs; + } + prev = current; + } + std::cout << "Number of increments: " << incrs << '\n'; + return EXIT_SUCCESS; +} diff --git a/2021/puzzle-01-02.cc b/2021/puzzle-01-02.cc new file mode 100644 index 0000000..953b056 --- /dev/null +++ b/2021/puzzle-01-02.cc @@ -0,0 +1,32 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2021. +// + +#include +#include +#include +#include +#include + +int main(void) { + std::vector nums; + unsigned incrs{0}; + std::string line; + while (std::getline(std::cin, line)) { + nums.push_back(std::stoul(line)); + } + assert(nums.size() >= 3); + unsigned long prev{std::numeric_limits::max()}; + for (std::size_t pos = 0; pos <= nums.size() - 3; ++pos) { + unsigned long current{0}; + for (std::size_t i = pos + 0; i < pos + 3; ++i) { + current += nums[i]; + } + if (current > prev) { + ++incrs; + } + prev = current; + } + std::cout << "Number of increments: " << incrs << '\n'; + return EXIT_SUCCESS; +}