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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#if 0
#define PFILE "sample.txt"
#else
#define PFILE "input.txt"
#endif
#define T_CAP 100
char template[T_CAP];
size_t tmpl_sz = 0;
#define R_CAP 100
typedef struct {
char inp[2];
char ch;
} rule_t;
rule_t rules[R_CAP];
size_t rules_sz = 0;
#define CAP 1000000
typedef uint64_t lsize_t;
char product[CAP];
lsize_t prod_sz = 0;
void Parse()
{
// some init
memset(template, '\0', T_CAP);
memset(product, '\0', CAP);
char ch;
FILE *fp;
fp = fopen(PFILE, "r");
int mode = 0; // 0 - for template; 1 - for rules; 2 - intermid
int mode_s = 0; // 0 - has not encountered ' '; 1 - after ' '
while((ch = fgetc(fp)) != EOF)
{
if(ch == '\n' && mode == 0)
{
mode = 2;
}
else if(ch == '\n' && mode == 2)
{
mode = 1;
}
else if(ch == '\n' && mode == 1)
{
memset(rules[rules_sz].inp, '\0', 2);
rules[rules_sz].ch = '\0';
}
else if(ch != '\n' && mode == 0)
{
template[tmpl_sz] = ch;
tmpl_sz += 1;
}
else if(ch != '\n' && mode == 1)
{
switch(mode_s)
{
case 0:
if(rules[rules_sz].inp[0] == '\0')
{
rules[rules_sz].inp[0] = ch;
}
else
{
rules[rules_sz].inp[1] = ch;
mode_s = 1;
}
break;
case 1:
if(ch != ' ' && ch != '-' && ch != '>')
{
rules[rules_sz].ch = ch;
rules_sz += 1;
mode_s = 0;
}
}
}
}
fclose(fp);
}
void InsertAt(char ch, size_t pos)
{
for(lsize_t i = prod_sz; i > pos; i--)
{
product[i] = product[i-1];
}
product[pos] = ch;
prod_sz += 1;
}
void Part1()
{
Parse();
prod_sz = tmpl_sz;
memcpy(product, template, prod_sz);
char elements[CAP]; memset(elements, '\0', CAP);
lsize_t positions[CAP];
lsize_t inrt_sz = 0;
printf("%s\n", product);
for(int step = 0; step < 40; step++)
{
for(lsize_t i = 0; i < (prod_sz-1); i++)
{
for(int j = 0; j < rules_sz; j++)
{
if(product[i] == rules[j].inp[0] &&
product[i+1] == rules[j].inp[1])
{
elements[inrt_sz] = rules[j].ch;
positions[inrt_sz] = i + 1 + inrt_sz; //error potential
inrt_sz += 1;
break;
}
}
}
for(lsize_t i = 0; i < inrt_sz; i++)
{
InsertAt(elements[i], positions[i]);
}
inrt_sz = 0;
memset(elements, '\0', CAP);
}
// Get count and subtract
lsize_t el_occurence[10];
/* S - 0 */
/* H - 1 */
/* N - 2 */
/* C - 3 */
/* O - 4 */
/* P - 5 */
/* F - 6 */
/* B - 7 */
/* V - 8 */
/* K - 9 */
for(lsize_t i = 0; i < prod_sz; i++)
{
switch(product[i])
{
case 'S': el_occurence[0] += 1; break;
case 'H': el_occurence[1] += 1; break;
case 'N': el_occurence[2] += 1; break;
case 'C': el_occurence[3] += 1; break;
case 'O': el_occurence[4] += 1; break;
case 'P': el_occurence[5] += 1; break;
case 'F': el_occurence[6] += 1; break;
case 'B': el_occurence[7] += 1; break;
case 'V': el_occurence[8] += 1; break;
case 'K': el_occurence[9] += 1; break;
}
}
//get biggest
lsize_t biggest = 0;
for(int i = 0; i < 10; i++)
{
int counter = 0;
for(int j = 0; j < 10; j++)
if(el_occurence[i] >= el_occurence[j]) counter += 1;
if(counter == 10) { biggest = el_occurence[i]; break; }
}
//get smallest
lsize_t smallest = 0;
for(int i = 0; i < 10; i++)
{
int counter = 0;
for(int j = 0; j < 10; j++)
if(el_occurence[i] <= el_occurence[j]) counter += 1;
if(counter == 10) {smallest = el_occurence[i]; break; }
}
lsize_t result = biggest - smallest;
printf("%llu-%llu=%llu", biggest, smallest, result);
}
void Part2()
{
}
int main(void)
{
Part1();
return 0;
}
|