Add 2022 Day 3.

This commit is contained in:
2022-12-03 08:26:18 +00:00
parent 7005eb7ff4
commit 46b36d7ec8
3 changed files with 81 additions and 0 deletions

32
2022/puzzle-03-01.cc Normal file
View File

@@ -0,0 +1,32 @@
//
// Created by Matthew Gretton-Dann on 02/12/2022.
//
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
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;
}