From 8efbb25e294e0e6426120e7aed2b46443f36166b Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Tue, 14 Dec 2021 09:04:18 +0100 Subject: [PATCH] part one day 14 --- 13b.c | 14 ++++---- 14.txt | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 14a.c | 58 ++++++++++++++++++++++++++++++++ 14e.txt | 18 ++++++++++ 4 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 14.txt create mode 100644 14a.c create mode 100644 14e.txt diff --git a/13b.c b/13b.c index 092cf7a..dafd0ea 100644 --- a/13b.c +++ b/13b.c @@ -1,5 +1,4 @@ #include -#include #include @@ -49,15 +48,14 @@ int main() *(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; diff --git a/14.txt b/14.txt new file mode 100644 index 0000000..6808cb7 --- /dev/null +++ b/14.txt @@ -0,0 +1,102 @@ +KHSSCSKKCPFKPPBBOKVF + +OS -> N +KO -> O +SK -> B +NV -> N +SH -> V +OB -> V +HH -> F +HP -> H +BP -> O +HS -> K +SN -> B +PS -> C +BS -> K +CF -> H +SO -> C +NO -> H +PP -> H +SS -> P +KV -> B +KN -> V +CC -> S +HK -> H +FN -> C +OO -> K +CH -> H +CP -> V +HB -> N +VC -> S +SP -> F +BO -> F +SF -> H +VO -> B +FF -> P +CN -> O +NP -> H +KK -> N +OP -> S +BH -> F +CB -> V +HC -> P +KH -> V +OV -> V +NK -> S +PN -> F +VV -> N +HO -> S +KS -> C +FP -> F +FH -> F +BB -> C +FB -> V +SB -> K +KP -> B +FS -> C +KC -> P +SC -> C +VF -> F +VN -> B +CK -> C +KF -> H +NS -> C +FV -> K +HV -> B +HF -> K +ON -> S +CV -> N +BV -> F +NB -> N +NN -> F +BF -> N +VB -> V +VS -> K +BK -> V +VP -> P +PB -> F +KB -> C +VK -> O +NF -> F +FO -> F +PH -> N +VH -> B +HN -> B +FK -> K +PO -> H +CO -> B +FC -> V +OK -> F +OF -> V +PF -> F +BC -> B +BN -> O +NC -> K +SV -> H +OH -> B +PC -> O +OC -> C +CS -> P +PV -> V +NH -> C +PK -> H diff --git a/14a.c b/14a.c new file mode 100644 index 0000000..c97d360 --- /dev/null +++ b/14a.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#include + +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); +} diff --git a/14e.txt b/14e.txt new file mode 100644 index 0000000..b5594dd --- /dev/null +++ b/14e.txt @@ -0,0 +1,18 @@ +NNCB + +CH -> B +HH -> N +CB -> H +NH -> C +HB -> C +HC -> B +HN -> C +NN -> C +BH -> H +NC -> B +NB -> B +BN -> B +BB -> N +BC -> B +CC -> N +CN -> C -- 2.20.1