-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc2.c
More file actions
89 lines (85 loc) · 2.32 KB
/
Copy pathaoc2.c
File metadata and controls
89 lines (85 loc) · 2.32 KB
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
#include "aoc2.h"
#include "../common.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
Game *parse_game(char *game) {
int sets = Common_count_occurances(game, ';') + 1;
Game *g = malloc(sizeof(int) + sets*sizeof(Set));
Common_strip_string(&game, "Game ");
g->index = Common_get_number(&game);
Common_strip_string(&game, ": ");
int i = 0;
while (*game != '\0') {
Set *s = malloc(sizeof(Set));
while (true) {
int amount = Common_get_number(&game);
Common_strip_string(&game, " ");
if (Common_string_has_prefix(game, "red")) {
s->red += amount;
Common_strip_string(&game, "red");
}
if (Common_string_has_prefix(game, "blue")) {
s->blue += amount;
Common_strip_string(&game, "blue");
}
if (Common_string_has_prefix(game, "green")) {
s->green += amount;
Common_strip_string(&game, "green");
}
if (*game == ';') {
Common_strip_string(&game, "; ");
break;
}
if (*game == '\0') {
break;
}
Common_strip_string(&game, ", ");
}
g->sets[i++] = *s;
free(s);
}
g->set_count = i;
return g;
}
bool is_valid_game(Game *game) {
for (int i = 0; i < game->set_count; i++) {
Set s = game->sets[i];
if (s.red > 12 || s.green > 13 || s.blue > 14) {
return false;
}
}
return true;
}
int calculate_power(Game *game) {
int mr = 0;
int mg = 0;
int mb = 0;
for (int i = 0; i < game->set_count; i++) {
Set s = game->sets[i];
if (mr < s.red) {
mr = s.red;
}
if (mg < s.green) {
mg = s.green;
}
if (mb < s.blue) {
mb = s.blue;
}
}
return mr * mg * mb;
}
void Day2_solve() {
Node *head = Common_readFile("AoC2/day2.txt");
int sum = 0;
int sum_power = 0;
do {
Game *g = parse_game(head->value);
sum_power += calculate_power(g);
if (is_valid_game(g)) {
sum += g->index;
}
} while (head = head->next);
printf("Answer 1st: %i\n", sum);
printf("Answer 2nd: %i\n", sum_power);
}