From d269a2a84045f6cd904af74cd379640de15a06fb Mon Sep 17 00:00:00 2001 From: Mia Herkt Date: Tue, 14 Dec 2021 21:14:00 +0100 Subject: [PATCH] Day 14: Add C solution --- 14/input.txt | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ 14/solution.c | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 14/input.txt create mode 100644 14/solution.c diff --git a/14/input.txt b/14/input.txt new file mode 100644 index 0000000..592999d --- /dev/null +++ b/14/input.txt @@ -0,0 +1,102 @@ +FSKBVOSKPCPPHVOPVFPC + +BV -> O +OS -> P +KP -> P +VK -> S +FS -> C +OK -> P +KC -> S +HV -> F +HC -> K +PF -> N +NK -> F +SC -> V +CO -> K +PO -> F +FB -> P +CN -> K +KF -> N +NH -> S +SF -> P +HP -> P +NP -> F +OV -> O +OP -> P +HH -> C +FP -> P +CS -> O +SK -> O +NS -> F +SN -> S +SP -> H +BH -> B +NO -> O +CB -> N +FO -> N +NC -> C +VF -> N +CK -> C +PC -> H +BP -> B +NF -> O +BB -> C +VN -> K +OH -> K +CH -> F +VB -> N +HO -> P +FH -> K +PK -> H +CC -> B +VH -> B +BF -> N +KS -> V +PV -> B +CP -> N +PB -> S +VP -> V +BO -> B +HS -> H +BS -> F +ON -> B +HB -> K +KH -> B +PP -> H +BN -> C +BC -> F +KV -> K +VO -> P +SO -> V +OF -> O +BK -> S +PH -> V +SV -> F +CV -> H +OB -> N +SS -> H +VV -> B +OO -> V +CF -> H +KB -> F +NV -> B +FV -> V +HK -> P +VS -> P +FF -> P +HN -> N +FN -> F +OC -> K +SH -> V +KO -> C +HF -> B +PN -> N +SB -> F +VC -> B +FK -> S +KK -> N +FC -> F +NN -> P +NB -> V +PS -> S +KN -> S diff --git a/14/solution.c b/14/solution.c new file mode 100644 index 0000000..97d6a91 --- /dev/null +++ b/14/solution.c @@ -0,0 +1,68 @@ +#include +#include + +/* we can pack the pairs into integers… */ +#define P(a,b) (( (a-'A')<<6 )| (b-'A')) + +/* …and use them as keys for a lookup table */ +enum {maxk=P('Z','Z')+1}; +size_t counts[maxk] = {0}, tcounts[maxk]; + +void pcounts(const char *s) { + size_t min=-1,max=0,k,n,i; + for(i='A';i<'Z'+1;i++) { + k=P(i,'z'); + if ((n=counts[k])){ + if (nmax) max=n; + } + } + printf("%s: %lu\n", s, max-min); +} + +int main(void) { + char tmpl[128], rules[128][3]={0}; + size_t i,j,n,k; + + scanf("%127[A-Z]\n", tmpl); + for(i=0;tmpl[i];i++){ + /* set initial counts for pairs and single chars */ + if(i) counts[P(tmpl[i-1],tmpl[i])]++; + /* use the same LUT for both */ + counts[P(tmpl[i],'z')]++; + } + + for(i=0;scanf("%c%c -> %c\n", + &rules[i][0], &rules[i][1], &rules[i][2])==3;i++); + + for(i=0;i<40;i++) { + /* reset temp LUT */ + memset(tcounts, 0, maxk*sizeof(size_t)); + + /* apply each rule */ + for(j=0;rules[j][0];j++) { + k=P(rules[j][0],rules[j][1]); + + if ((n=counts[k])) { + counts[k]=0; + /* single chars can be updated here */ + counts[P(rules[j][2],'z')]+=n; + /* pairs must be counted independently */ + tcounts[P(rules[j][0],rules[j][2])]+=n; + tcounts[P(rules[j][2],rules[j][1])]+=n; + } + } + + /* commit temp results */ + for(j=0;rules[j][0];j++) { + k=P(rules[j][0],rules[j][2]); + counts[k] = tcounts[k]; + k=P(rules[j][2],rules[j][1]); + counts[k] = tcounts[k]; + } + + if (i==9) + pcounts("Silver"); + } + pcounts("Gold"); +}