-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_2.c
120 lines (100 loc) · 2.32 KB
/
day_2.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
111
112
113
114
115
116
117
118
119
120
// https://adventofcode.com/2023/day/2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int red;
int green;
int blue;
} Stats;
char buf[255];
int parse(const char* line, Stats constraint) {
int game_idx;
line += 5; // skip Game
sscanf(line, "%d", &game_idx);
printf("%d: ", game_idx);
line = strchr(line, ':'); // find :
line+=2; // skip : and space
while(line != NULL && line[0] != '\0') {
int v;
sscanf(line, "%d", &v);
printf("%d=", v);
line = strchr(line, ' '); // find space
line++; // skip space
sscanf(line, "%s", buf);
printf("%s ", buf);
switch (buf[0]) {
case 'r':
if (v > constraint.red) return 0;
break;
case 'g':
if (v > constraint.green) return 0;
break;
case 'b':
if (v > constraint.blue) return 0;
break;
default:
printf("BAD TOKEN: '%s'\n", buf);
break;
}
line += strlen(buf);
if (line[0] == ' ') line++;
if (line[0] == '\n' || line[0] == '\0') break;
}
printf("\n");
return game_idx;
}
void task1() {
char line[255];
Stats constraint = {.red=12, .green=13, .blue=14};
int res = 0;
while(fgets(line, sizeof line, stdin) != NULL) {
res += parse(line, constraint);
}
printf("Result = %d\n", res);
}
int parse2(char* line) {
line = strchr(line, ':'); // find :
line += 2; // skip : and space
Stats stats= {.red=0, .green=0, .blue=0};
while(line != NULL && line[0] != '\0') {
int v;
sscanf(line, "%d", &v);
printf("%d=", v);
line = strchr(line, ' '); // find space
line++; // skip space
sscanf(line, "%s", buf);
printf("%s ", buf);
switch (buf[0]) {
case 'r':
if (v > stats.red) stats.red = v;
break;
case 'g':
if (v > stats.green) stats.green = v;
break;
case 'b':
if (v > stats.blue) stats.blue = v;
break;
default:
printf("BAD TOKEN: '%s'\n", buf);
break;
}
line += strlen(buf);
if (line[0] == ' ') line++;
if (line[0] == '\n' || line[0] == '\0') break;
}
printf("\n");
return stats.red * stats.green * stats.blue;
}
void task2() {
char line[255];
int res = 0;
while(fgets(line, sizeof line, stdin) != NULL) {
res += parse2(line);
}
printf("Result = %d\n", res);
}
int main(void) {
// task1();
task2();
}