-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.c
53 lines (48 loc) · 1.42 KB
/
1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
// extract number inspired by https://github.com/janiorca/advent-of-code-2023/blob/main/aoc1.c
#define LINE_LENGTH 256
const char nums[9][6] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
int char_to_num(char* c, bool check_words) {
if ( isdigit(*c) ) return *c - '0';
if ( !check_words ) return -1;
for (size_t i = 0; i < 9; i++)
{
if (strncmp(c, nums[i], strlen(nums[i])) == 0) return i+1;
}
return -1;
}
int extract_num_from_line(char *line, bool check_words) {
char *first = line;
while (char_to_num(first, check_words) == -1) first++;
char *last = line+strlen(line);
while (char_to_num(last, check_words) == -1) last--;
return 10*(char_to_num(first, check_words)) + char_to_num(last, check_words);
}
int main(int argc, char** argv)
{
// PART 1
(void)argc; //Cast to get rid of compiler warning
char filename[LINE_LENGTH];
strcpy(filename, argv[1]);
FILE *fp = fopen(filename, "r");
char line[LINE_LENGTH];
int sum = 0;
while( fgets(line, LINE_LENGTH, fp) != NULL) {
sum += extract_num_from_line(line, false);
}
printf("Part 1: %d\n", sum);
fclose(fp);
// PART 2
fp = fopen(filename, "r");
sum = 0;
while( fgets(line, LINE_LENGTH, fp) != NULL) {
int n = extract_num_from_line(line, true);
sum += n;
}
printf("Part 1: %d\n", sum);
fclose(fp);
return 0;
}