feat(01): Solved part 2
This commit is contained in:
parent
6f9d90bf4d
commit
c64b0d008c
@ -998,3 +998,4 @@ lpncsfkn7fsgvkl
|
||||
2four3threesxxvlfqfive4
|
||||
nine6eightsevenzx9twoxc
|
||||
hmbfjdfnp989mfivefiverpzrjs
|
||||
|
@ -6,35 +6,74 @@
|
||||
* @author jiriks74
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
// #define _LINE_LEN (size_t *)100
|
||||
#define _LINE_LEN 100
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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
|
||||
* @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;
|
||||
|
@ -42,9 +42,40 @@ TEST_F(Trebuchet, AssignmentInput) {
|
||||
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"));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user