From 3179cbda8bf04ee89d4a7554dc837255e3153d72 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Tue, 5 Dec 2023 15:40:45 +0100 Subject: [PATCH] Created the advent of code day 1 challenge --- aoc/01.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 aoc/01.cpp diff --git a/aoc/01.cpp b/aoc/01.cpp new file mode 100644 index 0000000..4c7263e --- /dev/null +++ b/aoc/01.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +using namespace std; + +// Advent of Code 2023, Day 1 + +int main() { + ifstream input("input.txt"); + map numbers = { + {"1", "one"}, {"2", "two"}, {"3", "three"}, + {"4", "four"}, {"5", "five"}, {"6", "six"}, + {"7", "seven"}, {"8", "eight"}, {"9", "nine"} + }; + + int calibrationValue = 0; + + for (string line; getline(input, line);) { + string current_str; + for (char current : line) { + current_str.push_back(current); + + for (auto item : numbers) { + size_t index = current_str.find(item.second); + + while(index != std::string::npos){ + current_str.replace(index, item.second.length(), item.first); + index = current_str.find(item.second); + } + } + } + line = current_str; + + vector digits; + for (char current: line) { + if (isdigit(current)) digits.push_back(current); + } + + string calibrationLine; + calibrationLine.push_back(digits[0]); + calibrationLine.push_back(digits.size() > 1 ? digits[digits.size() - 1] : digits[0]); + + cout << "Calibration Value (" << line << "): " << calibrationLine << "\n"; + calibrationValue += stoi(calibrationLine); + } + + cout << "Calibration Value Sum: " << calibrationValue; + + return 0; +} \ No newline at end of file