solve Day01 with cpp

This commit is contained in:
WieErWill 2023-12-02 12:14:52 +01:00
parent 88d6e8b852
commit b166b0ef70
3 changed files with 134 additions and 2 deletions

34
.gitignore vendored
View File

@ -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

90
Day01/cpp/solution.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <cassert>
std::vector<int> extractDigits(const std::string& line) {
std::map<std::string, int> 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<int> 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<int> 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<std::string> testLines = {
"two1nine",
"eightwothree",
"abcone2threexyz",
"xtwone3four",
"4nineeightseven2",
"zoneight234",
"7pqrstsixteen"
};
std::vector<int> 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;
}

View File

@ -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