Day 14: Add C solution
This commit is contained in:
parent
e163794fdc
commit
d269a2a840
2 changed files with 170 additions and 0 deletions
102
14/input.txt
Normal file
102
14/input.txt
Normal file
|
@ -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
|
68
14/solution.c
Normal file
68
14/solution.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 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 (n<min) min=n;
|
||||
else if (n>max) 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");
|
||||
}
|
Loading…
Reference in a new issue