From 46b36d7ec85f3840bd6cfe6967525712790897e1 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Sat, 3 Dec 2022 08:26:18 +0000 Subject: [PATCH] Add 2022 Day 3. --- .idea/dictionaries/mgrettondann.xml | 3 ++ 2022/puzzle-03-01.cc | 32 ++++++++++++++++++++ 2022/puzzle-03-02.cc | 46 +++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 .idea/dictionaries/mgrettondann.xml create mode 100644 2022/puzzle-03-01.cc create mode 100644 2022/puzzle-03-02.cc diff --git a/.idea/dictionaries/mgrettondann.xml b/.idea/dictionaries/mgrettondann.xml new file mode 100644 index 0000000..890ba1f --- /dev/null +++ b/.idea/dictionaries/mgrettondann.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/2022/puzzle-03-01.cc b/2022/puzzle-03-01.cc new file mode 100644 index 0000000..66b1f45 --- /dev/null +++ b/2022/puzzle-03-01.cc @@ -0,0 +1,32 @@ +// +// Created by Matthew Gretton-Dann on 02/12/2022. +// + +#include +#include +#include +#include +#include + +using Int = std::int64_t; + +auto main() -> int +{ + std::string letters{" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}; + + Int score{0}; + std::string line; + while (std::getline(std::cin, line)) { + auto left{line.substr(0, line.size() / 2)}; + auto right{line.substr(line.size() / 2)}; + std::sort(left.begin(), left.end()); + std::sort(right.begin(), right.end()); + std::string result; + std::set_intersection(left.begin(), left.end(), right.begin(), right.end(), + std::back_inserter(result)); + assert(!result.empty()); + score += letters.find(result[0]); + } + std::cout << "Score: " << score << '\n'; + return EXIT_SUCCESS; +} diff --git a/2022/puzzle-03-02.cc b/2022/puzzle-03-02.cc new file mode 100644 index 0000000..5bf702b --- /dev/null +++ b/2022/puzzle-03-02.cc @@ -0,0 +1,46 @@ +// +// Created by Matthew Gretton-Dann on 02/12/2022. +// + +#include +#include +#include +#include +#include + +using Int = std::int64_t; + +auto main() -> int +{ + std::string letters{" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}; + + Int score{0}; + std::string line; + Int state{0}; + std::string current; + while (std::getline(std::cin, line)) { + std::sort(line.begin(), line.end()); + if (state == 0) { + current = line; + ++state; + } + else if (state == 1) { + std::string r; + std::set_intersection(current.begin(), current.end(), line.begin(), line.end(), + std::back_inserter(r)); + current = r; + ++state; + } + else if (state == 2) { + std::string r; + std::set_intersection(current.begin(), current.end(), line.begin(), line.end(), + std::back_inserter(r)); + state = 0; + assert(!r.empty()); + score += letters.find(r[0]); + } + } + assert(state == 0); + std::cout << "Score: " << score << '\n'; + return EXIT_SUCCESS; +}