-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.c
126 lines (106 loc) · 3.33 KB
/
day05.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
121
122
123
124
125
126
#include "basic.c"
b8 order_rules[100][100] = {0};
i32 add_ordering_rules(u8* content, i32 file_size) {
u8 state = 0;
i32 n1 = 0;
i32 n2 = 0;
for (i32 i = 0; i < file_size; i++) {
if (content[i] >= '0' && content[i] <= '9') {
if (state == 0) {
n1 *= 10;
n1 += content[i] - '0';
} else if (state == 1) {
n2 *= 10;
n2 += content[i] - '0';
}
} else if (content[i] == '|') {
state = 1;
} else if (content[i] == '\n') {
if (state == 0) {
// Done with ordering rules
return i;
} else {
// New ordering rule
order_rules[n1][n2] = 1;
n1 = 0;
n2 = 0;
state = 0;
}
}
}
return 0;
}
i32 part_1(u8* content, u32 file_size) {
i32 current_rule = 0;
b8 valid = true;
i32 sum = 0;
i32 updates[100];
i32 updates_count = 0;
i32 start_index = add_ordering_rules(content, file_size);
for (i32 i = start_index; i < file_size; i++) {
if (content[i] >= '0' && content[i] <= '9') {
current_rule *= 10;
current_rule += content[i] - '0';
} else if (content[i] == ',' || content[i] == '\n') {
for (i32 u = 0; u < updates_count; u++) {
if (order_rules[current_rule][updates[u]]) {
valid = false;
}
}
updates[updates_count++] = current_rule;
current_rule = 0;
if (content[i] == '\n') {
if (valid) {
sum += updates[updates_count/2];
}
valid = true;
updates_count = 0;
}
}
}
return sum;
}
i32 part_2(u8* content, u32 file_size) {
i32 current_rule = 0;
b8 valid = true;
i32 sum = 0;
i32 updates[100];
i32 updates_count = 0;
i32 start_index = add_ordering_rules(content, file_size);
for (i32 i = start_index; i < file_size; i++) {
if (content[i] >= '0' && content[i] <= '9') {
current_rule *= 10;
current_rule += content[i] - '0';
} else if (content[i] == ',' || content[i] == '\n') {
updates[updates_count++] = current_rule;
current_rule = 0;
if (content[i] == '\n') {
for (i32 u = 0; u < updates_count; u++) {
for (i32 u2 = 0; u2 < u; u2++) {
if (order_rules[updates[u]][updates[u2]]) {
valid = false;
i32 temp = updates[u-1];
updates[u-1] = updates[u];
updates[u] = temp;
u = 0;
break;
}
}
}
if (!valid) {
sum += updates[updates_count/2];
}
valid = true;
updates_count = 0;
}
}
}
return sum;
}
i32 main() {
u32 file_size;
u8* content = load_file("input05.txt", &file_size);
printf("part 1: %i\n", part_1(content, file_size));
printf("part 2: %i\n", part_2(content, file_size));
return 0;
}