-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.c
110 lines (92 loc) · 3.03 KB
/
day01.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
long sum_of_calibration_values(const char *filename);
long sum_of_calibration_values_letters(const char *filename);
int main()
{
// Perform tests for part 1 and 2 by using the example data.
long result_example_1 = sum_of_calibration_values("inputs/day01-example.txt");
if (result_example_1 != 142) {
printf("Part 1 - Sum of all calibration values is not equal to 142 but: %ld", result_example_1);
exit(EXIT_FAILURE);
}
long result_example_2 = sum_of_calibration_values_letters("inputs/day01-example2.txt");
if (result_example_2 != 281) {
printf("Part 2 - Sum of all calibration values is not equal to 281 but: %ld", result_example_2);
exit(EXIT_FAILURE);
}
// Compute puzzle answers of part 1 and 2.
long result_part1 = sum_of_calibration_values("inputs/day01.txt");
long result_part2 = sum_of_calibration_values_letters("inputs/day01.txt");
printf("Part 1 - Sum of calibration values = %ld\n", result_part1); // 54304
printf("Part 2 - Sum of calibration values = %ld\n", result_part2); // 54418
exit(EXIT_SUCCESS);
}
long sum_of_calibration_values(const char *filename)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(filename, "r");
if (fp == NULL) {
exit(EXIT_FAILURE);
}
long sum = 0;
while ((read = getline(&line, &len, fp)) != -1) {
char result[3] = {0};
for (size_t i = 0; i < strlen(line); i++) {
if (isdigit(line[i])) {
if (!result[0]) result[0] = line[i];
result[1] = line[i];
}
}
sum += atol(result);
}
free(line);
fclose(fp);
return sum;
}
char parse_digit(const char *str)
{
size_t len = strlen(str);
if (len >= 1 && isdigit(str[0])) return str[0];
if (len >= 3 && !strncmp(str, "one", 3)) return '1';
if (len >= 3 && !strncmp(str, "two", 3)) return '2';
if (len >= 5 && !strncmp(str, "three", 5)) return '3';
if (len >= 4 && !strncmp(str, "four", 4)) return '4';
if (len >= 4 && !strncmp(str, "five", 4)) return '5';
if (len >= 3 && !strncmp(str, "six", 3)) return '6';
if (len >= 5 && !strncmp(str, "seven", 5)) return '7';
if (len >= 5 && !strncmp(str, "eight", 5)) return '8';
if (len >= 4 && !strncmp(str, "nine", 4)) return '9';
return 0;
}
long sum_of_calibration_values_letters(const char *filename)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(filename, "r");
if (fp == NULL) {
exit(EXIT_FAILURE);
}
long sum = 0;
while ((read = getline(&line, &len, fp)) != -1) {
char result[3] = {0};
for (size_t i = 0; i < strlen(line); i++) {
char d = parse_digit(line + i);
if (d) {
if (!result[0]) result[0] = d;
result[1] = d;
}
}
sum += atol(result);
}
free(line);
fclose(fp);
return sum;
}