feat(01): Solved part 1
This commit is contained in:
parent
baf227ce23
commit
118f3632a9
8 changed files with 3957 additions and 36 deletions
|
@ -5,21 +5,71 @@
|
|||
* @brief Main entry point
|
||||
* @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 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;
|
||||
for (int i = 0; i < strlen(str); i++) {
|
||||
if (str[i] >= *"0" && str[i] <= *"9") {
|
||||
if (num == 0) {
|
||||
num = (str[i] - 48) * 10;
|
||||
} else {
|
||||
lastNum = str[i] - 48;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lastNum == -1) {
|
||||
num += num / 10;
|
||||
} else {
|
||||
num += lastNum;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main entry point
|
||||
* @param argc Number of command-line arguments.
|
||||
* @param argv Array of command-line arguments.
|
||||
*/
|
||||
#ifndef TESTING
|
||||
#ifdef TESTING
|
||||
int mainTest(FILE *stdin, int argc, char *argv[])
|
||||
#else
|
||||
int main(int argc, char *argv[])
|
||||
#endif
|
||||
#ifdef TESTING
|
||||
int main_test(int argc, char *argv[])
|
||||
#endif
|
||||
{
|
||||
printf("Hello world!\n");
|
||||
return 0;
|
||||
FILE *file;
|
||||
if (argc == 1) {
|
||||
file = stdin;
|
||||
} else {
|
||||
file = fopen(argv[1], "r");
|
||||
}
|
||||
|
||||
if (file == NULL) {
|
||||
if (argc > 1)
|
||||
fprintf(stderr, "Could not open file '%s'\n", argv[1]);
|
||||
else
|
||||
fprintf(stderr, "Couldn't open file\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char *line;
|
||||
size_t len = 0;
|
||||
ssize_t read;
|
||||
int result = 0;
|
||||
while ((read = getline(&line, &len, file) != -1)) {
|
||||
result += getVal(line);
|
||||
}
|
||||
printf("The sum of the calibration values is %d\n", result);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue