-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDayOne_Trebuchet.c
55 lines (45 loc) · 1.04 KB
/
DayOne_Trebuchet.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
54
55
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main (int argc, char *argv[])
{
char *puzzle_filename = argv[1];
FILE *puzzle_fptr = fopen(puzzle_filename, "r");
if (puzzle_fptr == NULL)
{
printf("Error: could not open file %s", puzzle_filename);
return 1;
}
// reading line by line, max 256 bytes
const unsigned MAX_LENGTH = 256;
char buffer[MAX_LENGTH];
char curr;
char digits[3] = { 'a', 'a', '\0'};
int temp = 0;
int sum = 0;
while (fgets(buffer, MAX_LENGTH, puzzle_fptr))
{
for(int i = 0; i < strlen(buffer); i++){
curr = *(buffer + i);
if(isdigit(curr)){
digits[0] = curr;
break;
}
}
for(int i = strlen(buffer); i >= 0; i--){
curr = buffer[i];
if(isdigit(curr)){
digits[1] = curr;
break;
}
}
sscanf(digits, "%d", &temp);
printf("Adding %d to %d\n", temp, sum);
sum += temp;
digits[0] = 'a';
}
printf("Done decoding. Result is %d", sum);
// close the files
fclose(puzzle_fptr);
return 0;
}