feat(01): Solved part 2

This commit is contained in:
Jiří Štefka 2023-12-11 04:23:14 +01:00
parent 6f9d90bf4d
commit c64b0d008c
Signed by: jiriks74
GPG Key ID: 1D5E30D3DB2264DE
3 changed files with 88 additions and 15 deletions

View File

@ -998,3 +998,4 @@ lpncsfkn7fsgvkl
2four3threesxxvlfqfive4 2four3threesxxvlfqfive4
nine6eightsevenzx9twoxc nine6eightsevenzx9twoxc
hmbfjdfnp989mfivefiverpzrjs hmbfjdfnp989mfivefiverpzrjs

View File

@ -6,35 +6,74 @@
* @author jiriks74 * @author jiriks74
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
// #define _LINE_LEN (size_t *)100
#define _LINE_LEN 100
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
/**
* @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 * @brief Gets a calibration value from a line
* @param char* line containing the calibration value * @param char* line containing the calibration value
* @return int the calibration value * @return int the calibration value
*/ */
int getVal(char *str) { int getVal(const char *str) {
int num = 0; int result = 0;
int lastNum = -1; int result2 = -1;
for (int i = 0; i < strlen(str); i++) { for (int i = 0; i < strlen(str); i++) {
if (str[i] >= *"0" && str[i] <= *"9") { int num = getNum(&str[i]);
if (num == 0) { if (num >= 0) {
num = (str[i] - 48) * 10; if (result == 0) {
result = num * 10;
} else { } else {
lastNum = str[i] - 48; result2 = num;
} }
} }
} }
if (lastNum == -1) { if (result2 == -1) {
num += num / 10; result += result / 10;
} else { } else {
num += lastNum; result += result2;
} }
return num; return result;
} }
/** /**
@ -52,9 +91,10 @@ int main(int argc, char *argv[])
if (argc == 1) { if (argc == 1) {
file = stdin; file = stdin;
} else { } else {
file = fopen(argv[1], "r"); file = fopen(argv[1], "r"); // LCOV_EXCL_LINE
} }
// LCOV_EXCL_START
if (file == NULL) { if (file == NULL) {
if (argc > 1) if (argc > 1)
fprintf(stderr, "Could not open file '%s'\n", argv[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"); fprintf(stderr, "Couldn't open file\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// LCOV_EXCL_STOP
char *line; char *line;
size_t len = 0; size_t len = 0;

View File

@ -35,16 +35,47 @@ public:
TEST_F(Trebuchet, AssignmentInput) { TEST_F(Trebuchet, AssignmentInput) {
testing::internal::CaptureStdout(); testing::internal::CaptureStdout();
stream_add(input, "1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet"); stream_add(input, "1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet");
char* argv[2]; char *argv[2];
mainTest(input, 1, argv); mainTest(input, 1, argv);
std::string output = testing::internal::GetCapturedStdout(); std::string output = testing::internal::GetCapturedStdout();
// Check the output of the program. // Check the output of the program.
EXPECT_EQ("The sum of the calibration values is 142\n", output); 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) { TEST_F(Trebuchet, getVal) {
EXPECT_EQ(12, getVal((char *)"1abc2")); EXPECT_EQ(12, getVal((char *)"1abc2"));
EXPECT_EQ(38, getVal((char *)"pqr3stu8vwx")); EXPECT_EQ(38, getVal((char *)"pqr3stu8vwx"));
EXPECT_EQ(15, getVal((char *)"a1b2c3d4e5f")); EXPECT_EQ(15, getVal((char *)"a1b2c3d4e5f"));
EXPECT_EQ(77, getVal((char *)"treb7uchet")); 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"));
} }