From b166b0ef70bf33aa219775be1a9cb56484379c4c Mon Sep 17 00:00:00 2001 From: wieerwill Date: Sat, 2 Dec 2023 12:14:52 +0100 Subject: [PATCH] solve Day01 with cpp --- .gitignore | 34 ++++++++++++++++ Day01/cpp/solution.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++ README.md | 12 +++++- 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 Day01/cpp/solution.cpp diff --git a/.gitignore b/.gitignore index 44baee0..00f715f 100644 --- a/.gitignore +++ b/.gitignore @@ -174,4 +174,38 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +solution helper.md \ No newline at end of file diff --git a/Day01/cpp/solution.cpp b/Day01/cpp/solution.cpp new file mode 100644 index 0000000..a2a391d --- /dev/null +++ b/Day01/cpp/solution.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include + +std::vector extractDigits(const std::string& line) { + std::map digitMap = { + {"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}, + {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9} + }; + for (int i = 0; i <= 9; ++i) { + digitMap[std::to_string(i)] = i; + } + + std::vector digitsFound; + size_t i = 0; + while (i < line.length()) { + bool matched = false; + for (const auto& pair : digitMap) { + if (line.substr(i, pair.first.length()) == pair.first) { + digitsFound.push_back(pair.second); + i += pair.first.length() - 1; + matched = true; + break; + } + } + i++; + } + + return digitsFound; +} + +int extractCalibrationValue(const std::string& line) { + std::vector digits = extractDigits(line); + if (!digits.empty()) { + return digits.front() * 10 + digits.back(); + } + return 0; +} + +int calculateTotalCalibrationValue(const std::string& filePath) { + std::ifstream file(filePath); + if (!file.is_open()) { + throw std::runtime_error("Unable to open file: " + filePath); + } + + std::string line; + int totalValue = 0; + while (std::getline(file, line)) { + totalValue += extractCalibrationValue(line); + } + + return totalValue; +} + +void test() { + std::cout << "Starting Tests" << std::endl; + std::vector testLines = { + "two1nine", + "eightwothree", + "abcone2threexyz", + "xtwone3four", + "4nineeightseven2", + "zoneight234", + "7pqrstsixteen" + }; + std::vector expectedResults = {29, 83, 13, 24, 42, 14, 76}; + + for (size_t i = 0; i < testLines.size(); ++i) { + int result = extractCalibrationValue(testLines[i]); + assert(result == expectedResults[i]); + std::cout << "Line: '" << testLines[i] << "', Calibration Value: " << result << std::endl; + } + std::cout << "Finished Tests" << std::endl << std::endl; +} + +int main() { + try { + test(); + int totalValue = calculateTotalCalibrationValue("../input.txt"); + std::cout << "Total Calibration Value from input.txt: " << totalValue << std::endl; + } catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + } + + return 0; +} diff --git a/README.md b/README.md index 0608c98..0c0c38b 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ For example: ```bash cd Day01/python -python3 part2.py test.txt # for testing +python3 part2.py test.txt # for testing (not all Days will have that) python3 part2.py ``` @@ -61,7 +61,15 @@ You can either test or run the solution: ```bash cd Day01/rust cargo test #running tests -cargo run +cargo run # run with the actual input file +``` + +## C++ +Install a C++ compiler like g++ on your machine. Then: +```bash +cd Day01/cpp +g++ -o solution solution.cpp # compile +./solution #run the binary ``` ## JavaScript