diff options
author | Marvin Borner | 2021-12-14 15:53:56 +0100 |
---|---|---|
committer | Marvin Borner | 2021-12-14 15:57:55 +0100 |
commit | 883d350d37b4802a2569772853e1ea2a967972d0 (patch) | |
tree | 88a0783ed94a74ed928e067971302e98cae56fd4 /2021/14 | |
parent | ed341f7f190cec49d0c3d6353f63234ce5b58bef (diff) |
hee
Diffstat (limited to '2021/14')
-rw-r--r-- | 2021/14/Makefile | 19 | ||||
-rw-r--r-- | 2021/14/input | 102 | ||||
-rw-r--r-- | 2021/14/solve.c | 99 |
3 files changed, 220 insertions, 0 deletions
diff --git a/2021/14/Makefile b/2021/14/Makefile new file mode 100644 index 0000000..4a881b7 --- /dev/null +++ b/2021/14/Makefile @@ -0,0 +1,19 @@ +DEBUG = -Wno-error -Og -g -s -fsanitize=undefined -fsanitize=address -fstack-protector-all +CFLAGS = -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic-errors -Ofast + +# Not the best makefile but idc + +debug: + @gcc $(CFLAGS) $(DEBUG) solve.c -o solve.o -L/usr/lib -lcrypto + +build: + @gcc $(CFLAGS) solve.c -o solve.o -L/usr/lib -lcrypto + +clean: + @rm -f *.o + +run: debug + @./solve.o + +time: build + @./solve.o diff --git a/2021/14/input b/2021/14/input new file mode 100644 index 0000000..ac9c0aa --- /dev/null +++ b/2021/14/input @@ -0,0 +1,102 @@ +BCHCKFFHSKPBSNVVKVSK + +OV -> V +CO -> V +CS -> O +NP -> H +HH -> P +KO -> F +VO -> B +SP -> O +CB -> N +SB -> F +CF -> S +KS -> P +OH -> H +NN -> O +SF -> K +FH -> F +VV -> B +VH -> O +BV -> V +KF -> K +CC -> F +NF -> H +VS -> O +SK -> K +HV -> O +CK -> K +VP -> F +HP -> S +CN -> K +OB -> H +NS -> F +PS -> S +KB -> S +VF -> S +FP -> H +BB -> N +HF -> V +CH -> N +BH -> F +KK -> B +OO -> N +NO -> K +BP -> K +KH -> P +KN -> P +OF -> B +VC -> F +NK -> F +ON -> O +OC -> P +VK -> O +SH -> C +NH -> C +FB -> B +FC -> K +OP -> O +PV -> V +BN -> V +PC -> K +PK -> S +FF -> C +SV -> S +HK -> H +NB -> C +OK -> C +PH -> B +SO -> O +PP -> F +KV -> V +FO -> B +FN -> H +HN -> C +VB -> K +CV -> O +BC -> C +CP -> S +FS -> S +KP -> V +BS -> V +BK -> B +PN -> C +PF -> S +HO -> V +NC -> N +SS -> N +BO -> P +BF -> N +NV -> P +PB -> K +HB -> H +VN -> H +FV -> B +FK -> K +PO -> S +SC -> S +HS -> S +KC -> F +HC -> S +OS -> K +SN -> N diff --git a/2021/14/solve.c b/2021/14/solve.c new file mode 100644 index 0000000..b8c49f0 --- /dev/null +++ b/2021/14/solve.c @@ -0,0 +1,99 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#define COUNT(a) ((int)(sizeof(a) / sizeof 0 [a])) + +#define FORLINE \ + char *line = NULL; \ + size_t len = 0; \ + while (getline(&line, &len, fp) != EOF) +#define FREELINE \ + if (line) \ + free(line) + +#define IDX(a, b) (((a) - 'A') * 26 + ((b) - 'A')) +#define LEFT(i) (((i) / 26) + 'A') +#define RIGHT(i) (((i) % 26) + 'A') +static void solve(FILE *fp) +{ + char input[32] = { 0 }; + char rules[26 * 26] = { 0 }; + + int klaus = 0; + FORLINE + { + if (!klaus) + strcpy(input, line); + else if (klaus >= 2) + rules[IDX(line[0], line[1])] = line[6]; + klaus++; + } + FREELINE; + + long pairs[26 * 26] = { 0 }; + long counts[26] = { 0 }; + + for (char *p = input; *p && *p != '\n'; p++) { + if (*(p + 1)) + pairs[IDX(*p, *(p + 1))] += 1; + counts[*p - 'A'] += 1; + } + + int start = 0, end = 10; +aah: + for (int i = start; i < end; i++) { + long new[26 * 26] = { 0 }; + for (int j = 0; j < COUNT(pairs); j++) { + char a = LEFT(j), b = RIGHT(j); + char insert = rules[IDX(a, b)]; + if (insert) { + new[IDX(a, insert)] += pairs[j]; + new[IDX(insert, b)] += pairs[j]; + counts[insert - 'A'] += pairs[j]; + } else { + new[j] = pairs[j]; + } + }; + + memcpy(pairs, new, sizeof(pairs)); + } + + long max = 0, min = 0x4242424242424242; + for (int i = 0; i < COUNT(counts); i++) { + if (!counts[i]) + continue; + + if (counts[i] > max) + max = counts[i]; + else if (counts[i] < min) + min = counts[i]; + } + + printf("%lu\n", max - min); + + if (start == 0) { + start = end; + end = 40; + goto aah; + } +} + +int main(int argc, char *argv[]) +{ + (void)argc; + (void)argv; + + FILE *fp = fopen("input", "r"); + if (!fp) + exit(EXIT_FAILURE); + + clock_t tic = clock(); + solve(fp); + clock_t toc = clock(); + printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); + + fclose(fp); + return 0; +} |