summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMia Herkt <mia@0x0.st>2021-12-19 07:41:00 +0100
committerMia Herkt <mia@0x0.st>2021-12-19 07:41:00 +0100
commit4e22e92c78212f9df2b7337524054c24c4496403 (patch)
tree96917715d6f47417adfcf0f10ad7d980d14bb779
parent24d7d1b2bd8e03cfa3daa726db04a47fe73a62d0 (diff)
downloadaoc2021-4e22e92c78212f9df2b7337524054c24c4496403.tar.bz2
aoc2021-4e22e92c78212f9df2b7337524054c24c4496403.tar.xz
Day 18: Add C solution
-rw-r--r--18/input.txt100
-rw-r--r--18/solution.c54
2 files changed, 154 insertions, 0 deletions
diff --git a/18/input.txt b/18/input.txt
new file mode 100644
index 0000000..6fb0ffd
--- /dev/null
+++ b/18/input.txt
@@ -0,0 +1,100 @@
+[[[[7,1],[0,0]],[6,[8,2]]],[8,[3,8]]]
+[[[3,6],[9,4]],[[[5,9],5],[8,0]]]
+[[[2,2],2],[1,[[1,6],7]]]
+[[[[0,9],7],[[3,2],8]],[6,[7,9]]]
+[[[[4,1],6],[[7,6],[2,2]]],[[[1,1],9],4]]
+[[[8,[3,7]],3],[[4,4],[[9,1],[3,5]]]]
+[[4,[8,2]],[1,[0,5]]]
+[8,[8,7]]
+[[[[2,2],7],[3,[4,5]]],[[4,6],[[2,5],4]]]
+[[[5,5],[[5,1],3]],[[2,[8,2]],[[6,9],[1,5]]]]
+[0,7]
+[[[[5,1],3],[8,[5,3]]],7]
+[[5,[2,[0,6]]],[[[5,5],2],[9,[8,0]]]]
+[[[[3,4],2],0],4]
+[[[[5,3],[2,7]],6],[[4,0],[9,[7,2]]]]
+[[[3,[2,5]],[3,3]],7]
+[[[[5,1],1],[4,8]],[[5,[8,3]],2]]
+[[4,[[8,1],[8,5]]],[[[4,1],0],6]]
+[[[5,5],[5,9]],[0,[[6,8],[0,1]]]]
+[4,[[[7,9],4],0]]
+[[[[0,1],7],[[3,6],5]],[8,[5,[6,1]]]]
+[[[7,7],[8,0]],[6,[8,[7,9]]]]
+[[[9,2],1],6]
+[[[4,4],[2,[5,0]]],[[[2,6],6],[5,[4,3]]]]
+[[2,[[4,7],5]],1]
+[[8,7],[[[2,0],7],[1,[0,3]]]]
+[[9,[[9,3],[9,5]]],[[8,7],[[4,1],[6,5]]]]
+[[3,4],[[9,4],5]]
+[[5,[[8,3],5]],1]
+[[0,[[9,0],[3,2]]],[2,[7,[5,1]]]]
+[[9,[[9,5],[8,6]]],[[4,4],[[3,8],[1,6]]]]
+[[[1,[5,2]],9],[[4,6],[3,[8,0]]]]
+[[1,7],[[1,7],9]]
+[[[[3,4],3],[[7,5],[9,1]]],[[[5,0],[3,0]],[[7,9],6]]]
+[[[7,2],[[1,0],[5,6]]],[[[3,7],[8,9]],6]]
+[[[[1,1],1],[[8,6],[9,8]]],[[[1,8],4],[8,9]]]
+[[[8,9],0],3]
+[[[1,7],[1,[3,9]]],[6,[0,[8,5]]]]
+[[0,5],[6,5]]
+[[[[6,8],[4,5]],[[7,4],6]],[[3,6],5]]
+[[8,[[0,9],8]],[9,[7,[7,9]]]]
+[0,[[[7,1],2],[[0,4],4]]]
+[[0,[[9,1],5]],[1,4]]
+[3,4]
+[[[9,3],[1,3]],[[[4,8],3],[[1,3],[9,0]]]]
+[[[[5,1],7],[[9,2],8]],[[[6,8],[5,4]],[0,1]]]
+[8,[[1,[3,0]],[[7,9],4]]]
+[[[6,4],[[2,9],[9,0]]],[7,[[0,0],3]]]
+[[3,[[9,6],6]],2]
+[[5,[[3,1],[7,5]]],[[[6,7],9],[[4,6],[5,2]]]]
+[[[4,[6,5]],8],[[6,[8,0]],[[9,3],3]]]
+[[[[4,9],[2,8]],9],[[[5,0],0],[[3,4],[2,8]]]]
+[[3,[7,1]],[9,[[1,8],7]]]
+[[9,1],[0,[[0,7],[7,1]]]]
+[[7,[0,[7,6]]],[[[5,3],1],[6,[4,5]]]]
+[8,[[[2,1],[6,9]],[[3,3],[4,6]]]]
+[0,[7,[3,0]]]
+[[[[1,6],3],[5,[8,0]]],[[[6,6],7],1]]
+[[[7,[8,3]],3],[[[2,8],5],[0,[9,5]]]]
+[[[[5,1],4],[[1,2],1]],7]
+[[[3,[7,5]],7],3]
+[[9,[6,[1,1]]],[[[4,1],[2,2]],[[9,5],[7,7]]]]
+[2,7]
+[[[9,[8,6]],[[9,0],[6,5]]],[[[6,7],5],[[7,7],[2,3]]]]
+[[[0,[6,4]],2],[4,[7,[7,5]]]]
+[[[[6,1],[9,1]],[[6,1],9]],[[2,6],0]]
+[[0,[[1,8],[3,5]]],[4,[[8,2],[4,2]]]]
+[[[[9,3],[4,2]],2],[[[2,1],[7,1]],[4,8]]]
+[[[3,[0,2]],3],8]
+[[[4,[4,9]],9],[[[4,4],5],9]]
+[[[[8,2],7],9],[[[1,0],[3,8]],[[7,7],0]]]
+[[[3,2],[9,7]],[[9,[8,2]],[[5,5],3]]]
+[[[7,[3,1]],[[8,3],1]],[[[8,6],[7,0]],4]]
+[[9,[[9,1],5]],[[4,[1,1]],2]]
+[[[[7,4],[0,3]],7],[8,[6,[3,3]]]]
+[5,5]
+[[6,7],[1,[7,[8,1]]]]
+[[1,[0,4]],7]
+[[[4,0],[[0,1],[2,2]]],[9,[[9,9],[3,0]]]]
+[[[6,0],[[8,6],3]],[[5,1],[[8,1],[2,7]]]]
+[[[[8,3],7],5],[9,[[5,1],8]]]
+[[[[4,0],[5,2]],[[0,0],7]],2]
+[[[[0,1],6],2],[[8,2],6]]
+[[[[2,4],1],[[6,7],9]],[[[1,6],9],3]]
+[[5,5],[[8,[7,7]],[5,8]]]
+[[6,[[9,2],[9,7]]],[[[8,5],[4,4]],7]]
+[[[9,[7,7]],[6,0]],[7,[[8,7],[1,2]]]]
+[[7,[6,2]],[[9,[5,2]],[1,4]]]
+[[[7,[5,9]],[[3,9],[4,5]]],[0,6]]
+[[9,[8,[2,2]]],[[9,7],[1,1]]]
+[[[[2,3],4],[[4,8],9]],[[9,[8,6]],[[0,9],0]]]
+[[0,[[9,3],0]],[8,8]]
+[[[[2,9],6],[[2,8],9]],[[[0,5],6],[[6,1],7]]]
+[[9,[[8,3],[5,8]]],[[7,[3,0]],3]]
+[[[4,[4,2]],0],1]
+[[[[9,6],[5,8]],[6,2]],[[[8,0],[7,0]],[[5,6],4]]]
+[[[8,0],[[4,3],[7,4]]],[[3,[7,9]],[[7,3],6]]]
+[[3,[5,[0,3]]],[5,4]]
+[[[[1,2],[6,3]],1],[[7,[5,2]],[[8,8],7]]]
+[[4,[[8,0],[7,1]]],[[8,[8,0]],[[1,5],3]]] \ No newline at end of file
diff --git a/18/solution.c b/18/solution.c
new file mode 100644
index 0000000..3e57389
--- /dev/null
+++ b/18/solution.c
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+ typedef struct
+ N{ int n,l;}N;typedef
+ #define R(n,L) n->l=L,n->a\
+ =realloc(n->a,n->l* sizeof(N))
+ struct H{N*a;int l;}H ;int S
+ (H*n) {int i,j,r;for(i =0;i<n->
+ l;i ++)if(n->a[i].n>9){r=n->a[
+ i].n; n->a[i].n/=2;R(n,n->l+1);for
+ (j=n->l-1;j>i;j--)n->a[j]=n->a[j-1]; n->a[i+1].n=r-n->
+ a[ i ].n;n->a[i].l++;n->a[i+1].l++; return 1;}return 0;}void I(
+ char*s,H * n ){int l=0;for(;*s;s ++)if(*s>90)l+= 92- *s; else if(
+ *s>44){R(n,n->l+1);n-> a[n->l-1].n=*s- 48; n ->a [n->l -1 ]. l=l;}
+ }N*M(N*n,int l,int*o){int q,w=0,e=0; if(n ->l ==l)
+ q=n++->n;else{ n = M ( M(n,l+ 1,&w),l+1,&e);q= 3*w+2*e ;} *o+=q ;
+ return n ; } void C ( H * n ,H*r){R(n,r->l); memcpy(n->a ,r -> a,n-> l*
+ sizeof( N ) ) ; }void A(H*n,H*r){int i,o=n-> l;R(n,n ->l+r ->l );
+ memcpy( n ->a +o ,r -> a,r->l*sizeof(N)); for(i=0 ;i<n->l ;i ++)n ->a [i ]. l ++;
+do{int i , j ; for(i=0;i<n->l ;)if(n->a[i].l>=5){if(i )n-> a[i-1 ].n+= n ->a
+ [ i ] . n ; if(i+2<n->l)n->a[i+2].n+=n->a[ i+1 ] .n ; n
+ -> a[ i+ 1] .n =0 ;n -> a[ i +1].l--;for(j=i;j<n->l-1;j ++)n-> a[
+ j ]= n ->a [j +1 ]; n ->l -- ;}else{i++;}}while(S(n));}
+ int main(){char*a;H*h=NULL,r={0};
+ int k= 0, i, j ,S =0,G=0,g;for
+ (; scanf("%ms\n"
+ ,&a)==1;k
+ ++) { h=
+ realloc(h
+ ,( k+1)*
+ sizeof(H)
+ );memset(&h
+ [k ] , 0 ,
+ sizeof(H));
+ I(a,&h[k]);
+ free(a);}
+ for(i=0;i<k
+ ;i++)if(i)A
+ (&r,&h[i]);
+ else C(&
+ r,& h[i]);M
+ (&r.a [0],0,
+ &S) ; for(i=0;i<
+ k;i++){for(j=0;j<k ; j
+ ++){g=0;C(& r,&h[i ] );A(&r,&h[j]
+ );M (& r. a[0],0,&g ); G=g>G?g:
+ G;}}printf("Silve" """r: %"
+ """d\n""" "" "Gold:"
+ " %d"
+ "\n",S
+ ,G)
+ ;}