2
1
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
Mia Herkt 4a6bfff4c0
Day 14: Add obfuscated C solution 2021-12-14 22:35:27 +01:00
Mia Herkt d269a2a840
Day 14: Add C solution 2021-12-14 21:14:00 +01:00
3 changed files with 194 additions and 0 deletions

102
14/input.txt Normal file
View 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

24
14/obf.c Normal file
View File

@ -0,0 +1,24 @@
#include <stdio.h>
#include <string.h>
enum{M
#define\
P(a,\
b) (\
((a-\
65)<<\
6)|(b-65))
=P(90,90)+1};
size_t C[M]={0},T[M];void p(char*s){size_t r= -1,
R=0,k,n,i;for(i=65;i<90+1;i++){k=P(i,98);if((n=C[k])){if (n<r)
r= n;else if(n>R)R=n;}}printf("%s: %lu\n",s,R-r);}int main( void){
size_t i,j,n,k;char t[128],R[128][3]={0};scanf("%127[A-Z]\n",t) ;for(
i=0;t[i]/*⁻⁻*/;i++){if(i)/*⁻⁻*/C[P(t[i-1],t[i])]++;C[P(t[i],98)]++;}
for(i=0/* */;scanf(/* */"%c%c -> %c\n",&R[i][0],&R[i][1],&R[i
][2])==/* */3;i++);/* */for(i=0;i<40;i++){memset(T,0,M*sizeof
(size_t))/*₋₋*/;for(j=0;R[/*₋₋*/j][0];j++){k=P(R[j][0],R[j][1]);if((n=C
[k])){C[k]=0;C[P(R[j][2],98)]+=n;T[/*⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻*/P(R[j][0],R[j][
2])]+=n;T[P(R[j][2],R[j][1])]+=n;}}/* MADE IN CHINA */for(j=0; R[j][
0];j++){k=P(R[j][0],R[j][2]);C[k]/*₋₋₋₋₋₋₋₋₋₋₋₋₋₋₋*/=T[k]; k=P(R
[j][2],R[j][1]);C[k]=T[k];}if(i==9)p("Silver");}p("G" "old"
);}

68
14/solution.c Normal file
View 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");
}