From c64b0d008c44b545eb2dc847fe3c98aa0aca7b5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=A0tefka?= Date: Mon, 11 Dec 2023 04:23:14 +0100 Subject: [PATCH] feat(01): Solved part 2 --- 01/{assignment_input.txt => puzzle.input} | 1 + 01/src/main.c | 69 ++++++++++++++++++----- 01/tests/src/test.cpp | 33 ++++++++++- 3 files changed, 88 insertions(+), 15 deletions(-) rename 01/{assignment_input.txt => puzzle.input} (99%) diff --git a/01/assignment_input.txt b/01/puzzle.input similarity index 99% rename from 01/assignment_input.txt rename to 01/puzzle.input index 6deebe0..abd6cbc 100644 --- a/01/assignment_input.txt +++ b/01/puzzle.input @@ -998,3 +998,4 @@ lpncsfkn7fsgvkl 2four3threesxxvlfqfive4 nine6eightsevenzx9twoxc hmbfjdfnp989mfivefiverpzrjs + diff --git a/01/src/main.c b/01/src/main.c index e9bccd1..d097376 100644 --- a/01/src/main.c +++ b/01/src/main.c @@ -6,35 +6,74 @@ * @author jiriks74 */ #define _GNU_SOURCE -// #define _LINE_LEN (size_t *)100 -#define _LINE_LEN 100 #include #include #include +/** + * @brief + * param + * return + */ +int getNum(const char *line) { + char *str = (char *)line; + // Mapping of words to numeric representations + const char *numbers[] = {"zero", "one", "two", "three", "four", + "five", "six", "seven", "eight", "nine"}; + const char *digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; + + // Find first written number + char *smallestPos = strchr(str, *"\0"); + int numAtSmallPos = -1; + for (int i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++) { + char *pos = NULL; + + char *written = strstr(str, numbers[i]); + char *digit = strstr(str, digits[i]); + + if (written == NULL && digit == NULL) + continue; + if (written == NULL) + pos = digit; + else if (digit == NULL) + pos = written; + else + pos = digit < written ? digit : written; + + if (pos < smallestPos) { + numAtSmallPos = i; + smallestPos = pos; + } + } + + // Chen which was first and return it + return numAtSmallPos; +} + /** * @brief Gets a calibration value from a line * @param char* line containing the calibration value * @return int the calibration value */ -int getVal(char *str) { - int num = 0; - int lastNum = -1; +int getVal(const char *str) { + int result = 0; + int result2 = -1; for (int i = 0; i < strlen(str); i++) { - if (str[i] >= *"0" && str[i] <= *"9") { - if (num == 0) { - num = (str[i] - 48) * 10; + int num = getNum(&str[i]); + if (num >= 0) { + if (result == 0) { + result = num * 10; } else { - lastNum = str[i] - 48; + result2 = num; } } } - if (lastNum == -1) { - num += num / 10; + if (result2 == -1) { + result += result / 10; } else { - num += lastNum; + result += result2; } - return num; + return result; } /** @@ -52,9 +91,10 @@ int main(int argc, char *argv[]) if (argc == 1) { file = stdin; } else { - file = fopen(argv[1], "r"); + file = fopen(argv[1], "r"); // LCOV_EXCL_LINE } + // LCOV_EXCL_START if (file == NULL) { if (argc > 1) fprintf(stderr, "Could not open file '%s'\n", argv[1]); @@ -62,6 +102,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "Couldn't open file\n"); exit(EXIT_FAILURE); } + // LCOV_EXCL_STOP char *line; size_t len = 0; diff --git a/01/tests/src/test.cpp b/01/tests/src/test.cpp index 6527dd8..afb79d3 100644 --- a/01/tests/src/test.cpp +++ b/01/tests/src/test.cpp @@ -35,16 +35,47 @@ public: TEST_F(Trebuchet, AssignmentInput) { testing::internal::CaptureStdout(); stream_add(input, "1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet"); - char* argv[2]; + char *argv[2]; mainTest(input, 1, argv); std::string output = testing::internal::GetCapturedStdout(); // Check the output of the program. EXPECT_EQ("The sum of the calibration values is 142\n", output); } +TEST_F(Trebuchet, AssignmentInput2){ + testing::internal::CaptureStdout(); + stream_add(input, "two1nine\neightwothree\nabcone2threexyz\nxtwone3four\n4nineeightseven2\nzoneight234\n7pqrstsixteen"); + char *argv[2]; + mainTest(input, 1, argv); + std::string output = testing::internal::GetCapturedStdout(); + // Check the output of the program. + EXPECT_EQ("The sum of the calibration values is 281\n", output); +} + TEST_F(Trebuchet, getVal) { EXPECT_EQ(12, getVal((char *)"1abc2")); EXPECT_EQ(38, getVal((char *)"pqr3stu8vwx")); EXPECT_EQ(15, getVal((char *)"a1b2c3d4e5f")); EXPECT_EQ(77, getVal((char *)"treb7uchet")); + EXPECT_EQ(11, getVal((char *)"1")); +} + +TEST_F(Trebuchet, getNum) { + EXPECT_EQ(0, getNum((char *)"zero")); + EXPECT_EQ(1, getNum((char *)"one")); + EXPECT_EQ(2, getNum((char *)"two")); + EXPECT_EQ(3, getNum((char *)"three")); + EXPECT_EQ(4, getNum((char *)"four")); + EXPECT_EQ(5, getNum((char *)"five")); + EXPECT_EQ(6, getNum((char *)"six")); + EXPECT_EQ(7, getNum((char *)"seven")); + EXPECT_EQ(8, getNum((char *)"eight")); + EXPECT_EQ(9, getNum((char *)"nine")); + + EXPECT_EQ(1, getNum((char *)"1")); + EXPECT_EQ(1, getNum((char *)"one")); + EXPECT_EQ(1, getNum((char *)"1two")); + EXPECT_EQ(2, getNum((char *)"two1")); + + EXPECT_EQ(1, getNum((char *)"1threeone")); }