diff options
| -rw-r--r-- | 03/Makefile | 13 | ||||
| -rw-r--r-- | 03/part1.c | 43 |
2 files changed, 56 insertions, 0 deletions
diff --git a/03/Makefile b/03/Makefile new file mode 100644 index 0000000..a8f700f --- /dev/null +++ b/03/Makefile @@ -0,0 +1,13 @@ +all: part1 part2 + +part1: part1.c + gcc -g part1.c -o part1 -lalgds + +part2: part2.c + gcc -g part2.c -o part2 -lalgds + +1: part1 + cat input | ./part1 + +2: part2 + cat input | ./part2
\ No newline at end of file diff --git a/03/part1.c b/03/part1.c new file mode 100644 index 0000000..be73968 --- /dev/null +++ b/03/part1.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <string.h> + +#include <algds/str.h> +#include <algds/vec.h> + + +void getlines(StringVector *lines) { + while (1) { + char *line = fgetline(stdin); + if (line == NULL) { + break; + } + StringVector_push_back(lines, line); + } +} + +int find_max(const char *line) { + int len = strlen(line); + int max = 0; + for (int i = 0; i < len - 1; i++) { + for (int j = i + 1; j < len; j++) { + int n = (line[i] - '0') * 10 + line[j] - '0'; + if (n > max) { + max = n; + } + } + } + return max; +} + +int main() { + StringVector lines; + StringVector_init(&lines); + getlines(&lines); + String *it = StringVector_begin(&lines); + int sum = 0; + for (; it != StringVector_end(&lines); it++) { + sum += find_max(*it); + } + printf("%d\n", sum); + return 0; +}
\ No newline at end of file |
