#include <stdio.h>
-#include <stdbool.h>
#include <uthash.h>
*(to++)= '\0';
add_dot(&grid, pnt(atoi(buf), atoi(to)));
}
- while (getline(&buf, &len, stdin) != -1) {
- char *to = strchr(buf, '=');
- fold (&grid, *(to-1), atoi(to+1));
- }
int maxx = 0, maxy = 0;
- for (struct dot *d = grid; d!=NULL; d = d->hh.next) {
- maxx = maxx < d->p.x ? d->p.x : maxx;
- maxy = maxy < d->p.y ? d->p.y : maxy;
+ while (getline(&buf, &len, stdin) != -1) {
+ char *to = strchr(buf, '=')-1;
+ int foldline = atoi(to+2);
+ fold (&grid, *to, foldline--);
+ if (*to == 'x') maxx = foldline;
+ else maxy = foldline;
}
struct dot *d;
--- /dev/null
+#include <stdio.h>
+#include <stdbool.h>
+#include <limits.h>
+
+#include <uthash.h>
+
+struct pair { char x; char y; };
+struct template { struct pair key; char ins; UT_hash_handle hh; };
+
+int main()
+{
+ char *input = NULL, *buf = NULL, *oldinput = NULL;
+ size_t inputlen = 0, len;
+ struct template *template = NULL;
+
+ if (getline(&input, &inputlen, stdin) == -1)
+ return 1;
+ while (getline(&buf, &len, stdin) != -1) {
+ if (strcmp(buf, "\n") == 0)
+ continue;
+ struct template *t = calloc(1, sizeof(struct template));
+ t->key.x = buf[0];
+ t->key.y = buf[1];
+ t->ins = buf[6];
+ HASH_ADD(hh, template, key, sizeof(struct pair), t);
+ }
+
+ for (int i = 0; i<10; i++) {
+ free(oldinput);
+ oldinput = input;
+ input = malloc(inputlen *= 2);
+ inputlen = 0;
+ char *c;
+ for (c = &oldinput[0]; *c != '\0'; c++) {
+ input[inputlen++] = *c;
+ struct pair p = {.x=c[0], .y=c[1]};
+ struct template *t;
+ HASH_FIND(hh, template, &p, sizeof(struct pair), t);
+ if (t)
+ input[inputlen++] = t->ins;
+ }
+ input[inputlen++] = '\0';
+ }
+
+ int freq[255] = {0};
+ int mcommon = 0, lcommon = INT_MAX;
+ for (char *c = &input[0]; *c != '\0'; *c++) {
+ freq[*c]++;
+ }
+ for (int i = 0; i<255; i++) {
+ if (freq[i] > mcommon)
+ mcommon = freq[i];
+ if (freq[i] > 1 && freq[i] < lcommon)
+ lcommon = freq[i];
+ }
+
+ printf("%d\n", mcommon-lcommon);
+}