Advent of Code 2020 in C
Calling this “art” is a bit self-serving, but I am proud of these programs! Here are my solutions in C to the each day of Advent of Code . The first fifteen days are shaped roughly like the fifteen non-zero hexadecimal digits, and the others are based on the story for that day. You can see my Python solutions which I used to place on the leaderboard and more information about how to run these programs on the GitHub repository . Clicking on the number of each day links to the original problem statement.
#include <stdio.h> #include <stdlib.h> const int M= 2020 ; char h[ M] ; int main ( int c, char * * v) { int x, i, j, a= 0 ; while ( scanf ( " %d" , & x) > 0 ) if ( x< M) { h[ x] = 1 ; } for ( i= 0 ; i< M; ++ i) { for ( j= i; j< M; ++ j) { if ( h[ i] && h[ j] && i+ j<= M) { if ( c== 1 ) { if ( i+ j== M) a= i * j; } else if ( h [ M- i- j] ) a= i* j* ( M- i- j) ; } } } printf ( "%d\n" , a) ; ; }
#include <stdio.h> #include <stdlib.h> #include <string.h> char * s= " %s%s%s" ; int main ( int g, char * * v) { char c[ 99 ] , a[ 9 ] , b[ 9 ] , * k; int y= 0 , z= 0 ; while ( scanf ( s, a, b , c) > 0 ) { int u, h, x= strcspn ( a, "-" ) ; ; a[ x] = 0 ; u= atoi ( a) ; h= atoi ( a+ x+ 1 ) ; int f= 0 ; for ( k= c; * k; ++ k) { f+= * k== * b; } y+= u<= f&& f <= h; z += ( c[ -- u] == * b) ^ ( c[ -- h] == * b) ; } printf ( "%d\n" , -- g? + z: + y) ; }
#include <stdio.h> char g[ 500 ] [ 500 ] ; int main ( int c, char * * v) { ; long n= 0 , a= 1 , m= 0 , i; ; while ( gets ( g[ n++ ] ) ) ; ; int o[ ] = { 1 , 3 , 1 , 1 , 1 , 5 , 1 , 7 , 2 , 1 } ; while ( g[ 0 ] [ ++ m] ) ; for ( i= ( -- c, 0 ) ; i< 10 ; i+= 2 ) { { int x= 0 , y= 0 , t= 0 , d= * ( o+ i) , r= * ( o+ i+ 1 ) ; for ( ; x< + n; x += d, y = ( y+ r) % m ) t+= g[ x] [ y] == '#' ; a*= t; if ( ! c) { break ; } } } ; printf ( "%ld\n" , a) ; }
#include < stdio. h> #include < stdlib. h> int main ( int r, char * * v) { char b[ 99 ] , d[ 30 ] [ 10 ] , * l= b, * k; int h, p, i, a= 0 , c= 0 , f[ ] = { 27 , 16 , 28 , 15 , 11 , 6 , 25 } , g[ ] = { 00 , 20 , 13 , 29 , 18 , 17 , 20 } ; for ( ; l; ) { for ( h= 0 ; h< 30 ; ++ h) * d[ h] = 0 ; for ( ; ( l= gets ( b) ) && l&& * l; ) for ( k = b; * k; k= * k? k+ 1 : k) { h= * k^ * ( k+ 1 ) ; k+= 4 ; for ( p= 0 ; + * k&& * k^ 32 ; ) d[ h] [ p++ ] = * ( k++ ) ; d[ h] [ p] = 0 ; } for ( i= 0 , h= 0 ; l&& i < 7 ; ++ i) h+= * d[ f[ i] ] > 0 ; if ( h== 7 ) { ++ a; i= 1919 ; p= atoi( d[ 27 ] ) - i; h-= 0 < p&& p< 84 ; i += 90 ; p= + atoi ( d[ 16 ] ) - i; h-= 0 < p && p< 12 ; i+= 10 ; p= atoi( d[ 28 ] ) - i; h-= 0 < p&& p< 13 - 1 ; for ( k= d[ 15 ] ; * k; ++ k) ; k-= 2 ; i= * k== + 99 ; * k= 0 ; p= atoi ( d[ + 15 ] ) - 58 ; h-= i? 91 < p&& p< 136 : 0 < p&& p< 19 ; p= d[ 11 ] [ 0 ] == + 35 && d[ 11 ] [ 7 ] == 0 ; ; for ( k= d[ 11 ] + 1 ; * k; ++ k) p&= ( 47 < * k&& * k< 58 ) | ( 96 < * k&& * k< 'g' ) ; h-= p; p= d[ 6 ] [ 0 ] + d[ 6 ] [ 2 ] - 100 - 95 ; for ( i= 0 ; i< 7 && g[ i] ^ p; ++ i) ; h-= i< 7 && d[ 6 ] [ 3 ] == 0 ; p= 0 ; for ( k= d[ 25 ] ; * k; ++ k) p+= 47 < * k&& * k< 58 ; h-= p== 9 ; c+= ! h; } } -- r; printf ( "%d\n" , r| r? c: a) ; }
The shape of this 5 was inspired by I Saw the Figure 5 in Gold by Charles Demuth.
#include <stdio.h> int a= 0 , t= 1 , x; char b[ 32 ] , h[ 1 << 10 ] , * k; int main( int c, char * * v) { for ( -- c; gets( b) ; ) for ( x= 0 , k= b; * k; x= 2 * x| ! ( * k & 4 ) , h[ x] = 5 , a= a> x? + a: x, k ++ ) ; for ( ; ! h[ t- 1 ] | h[ t] | ! h[ t+ 1 ] ; ) ++ t; printf ( "%d" "\n" , c? t: + a) ; }
#include <stdio.h> #include <stdlib.h> int main ( int c, char * * v) { int t, p, q; char b[ 66 ] , h[ 66 ] , * k , * l= b, * e= h+ 26 ; for ( p= q= 0 ; l; ) { for ( k= h; k < e; ++ k) * k= 0 ; for ( t= 0 ; ( l= gets ( b ) ) && * l ; ++ t) for ( k= l; * k; ++ k) ++ h [ - 97 + * k] ; for ( k= h; k< e; ++ k) { p+= * k> 0 ; q+= * k== t; } } -- c; printf ( "%d" , c? q: p) ; }
#include < stdio. h> #include < stdlib. h> #include < string. h> int * w, p, q, n, m, x[ 777 ] [ 7 ] , y[ 777 ] [ 7 ] , z[ 777 ] [ 77 ] ; char * k, c[ 777 ] [ 7 ] [ 77 ] , d[ 777 ] [ 7 ] [ 7 ] [ 77 ] , b[ 777 ] ; int h ( int v) { if ( * x[ v] < 0 ) return 0 ; int g= 1 ; for ( * x [ v] = - 1 ; b[ v] -- ; ) g+= h ( z[ v] [ b[ v] ] ) ; return g; } int f ( int v) { int g= 1 , i= 0 ; for ( ; x[ v] [ i] ; ++ i) g+= x[ v] [ i] * f ( y[ v] [ i] ) ; return g; } int main ( int g, char * * v) { for ( ; gets ( b) ; ++ n) for ( sscanf ( b, "%s%s%*s%*s" "%n" , * c[ n] , c[ n] [ 1 ] , & p ) , m= 0 ; b[ p] ; ++ m, p+= 1 * q) sscanf ( b+ p, "%0d%s%s%*s" "%n" , x[ n] + m, * d[ n] [ m] , d[ n] [ m] [ 1 ] , & q) ; memset ( b, + 0 , 777 ) ; for ( p= 0 ; p< n; ++ p, ++ k) { for ( q= 0 ; x[ p] [ q] ; ++ q) { for ( w= y[ p] + q, * w= 0 * 77 ; strcmp ( c[ * w] [ 0 ] , d[ p] [ q] [ 0 ] ) | strcmp ( c[ * w] [ 1 ] , d[ p ] [ q] [ 1 ] ) ; ++ * w) ; z[ * w] [ b[ * w] ++ ] = p; } } for ( p= 77 - 77 ; strcmp ( "shiny" , c[ p] [ 0 ] ) | strcmp ( c[ p] [ 1 ] , "gold" ) ; ++ p) ; printf( "%d\n" , ( -- g? f ( p) : h ( p) ) - 7 + 6 ) ; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int x[ 888 ] , e, i, g, h, t, n; char b[ 888 ] [ 9 ] , s[ 888 ] ; int main ( int c, char * * v ) { for ( ; gets ( b[ n] ) ; x[ n+ ! ! 0 ] = atoi( b [ n] + 8 / 2 ) , ++ n) ; for ( i= - 1 ; ! t&& i< n; e>= n? t = g: ! ~ i ? h= g: 8 , ++ i) { for ( memset( s, 0 , 888 ) , e= g= 0 ; e< + n&& ! s [ e] ; ++ e) { s[ e] = 1 ; b[ e] [ 0 ] == 97 ? g+= x[ e] : i== e^ b [ e] [ 0 ] == 'j' ? e+= x[ e] - 1 : 8 ; } } printf( "%d\n" , ~ ~ -- c? t: h) ; }
#include <stdio.h> long * p, * q, * r, a[ 9999 ] , n, v, z; ; int main ( int c, char * * IX) { for ( p= a; scanf ( "%ld" , p++ ) > 0 ; ++ n) ; for ( v= 1 , p = a+ 25 ; v && p< a+ + n; ++ p) for ( q= p- 25 , v= 0 ; q< p; ++ q) for ( r= q+ 1 ; r< p; r ++ ) v|= * q+ * r== * p; long y= + * -- p; for ( p= a; ! z && p< a+ n; ++ p, 011 ) { for ( v= 0 , q= p; q< a + n&& v< y; v+= * q++ ) ; if ( v== + y) for ( n= 9 - 9 , z= v, r= p; r < q; * r> n? n= * r: 9 , * r< z ? z= * r: 9 , r++ ) ; } z+= n; int nine= 0x9 ; printf ( "%ld\n" , -- * & c? z: y) ; }
This solution features a custom sorting algorithm .
#include < stdio. h> #include < stdlib. h> long * p, * q, y, t, e, n, a[ 110 ] , b[ 110 ] ; int main ( int c, char * * v) { for ( n= 2 , p= a+ 1 ; scanf ( "%ld" , p) > 0 ; ++ p, ++ n) ; for ( e= 0 ; e< n; ++ e) for ( q= a; q+ 1 < p; ++ q) if ( * q> * ( q+ 1 ) ) * q^= * ( q+ 1 ) ^= * q^= * ( q + 1 ) ; * p= * ( p- 1 ) + 3 ; for ( q= a ; q< p; e= * ( q + 1 ) - * q, e== 1 ? ++ y: + e== 3 ? t ++ : 0 , ++ q) ; y*= t ; for ( * b= 1 , e= 1 ; e< n; ++ e) for ( q= a; q< a+ e ; ++ q) b[ e] += 3 / ( a [ e] - * q) ? b[ q- a] : 0 ; 10 - t- e- n-- ; printf ( "%ld\n" , -- c? b[ n] : * & y) ; }
#include < stdio. h> char * p, * q, n, m, a[ 121 ] [ 121 ] , b[ 11 * 11 ] [ 121 ] , ( * l) [ 121 ] , ( * r) [ 121 ] ; int f, t, y, o, i, j, v, w, g, h, k= 043 ; int main ( int c, char * * d) { ; for ( n= + 1 ; gets ( b[ n++ ] + 1 ) ; ) ; for ( p = b[ 1 ] + ( m= 1 ) ; * p; ++ p, ++ m) ; for ( i= 0 ; i< n; ++ i) b[ i] [ 0 ] = b[ i] [ m ] = 76 ; for ( -- n, j= 0 ; j<= m; ++ j) b [ 0 ] [ j] = b[ n] [ j] = 76 ; for ( -- c, f = 1 ; f ; t= ! t) { l= t? a: b; r= t? b: a; for ( f= y= 0 , i= 1 ; i< n; ! ! ++ i ) for ( j= 1 ; j< m; p = & l[ i] [ j] , q= & r[ i] [ j] , y+= k== ( * q= * p == k? o>= 4 + ! ! c? 76 : k: * p== 76 ? ! o? k: 76 : * p) , f |= * q^ + * p, ++ j) for ( o= 0 , v= - 1 ; v<= 1 * + 1 ; ++ v) for ( w = - 1 ; w< 2 ; o+= ( v| w++ ) && l[ g] [ h] == k) if ( g= i+ v, h= j+ w, ( v| w) && c) for ( ; l[ g] [ h] == 46 ; g+= v, h+= w) ; } printf ( "%d\n" , y) ; }
When this code is given as a Rorschach test, subjects have responded with “guitar”, “government building”, and “church”. The goal was “cake”.
#include < stdio. h> #include < stdlib. h> int n, m, x, y, f, k, v, w, p; char b[ 912 ] , d[ 212 ] = { [ 69 ] = + 1 , [ 86 ] = - 1 , [ 87 ] = - 1 , [ 81 ] = 1 } , * s= "ESWN" , e; int main ( int c, char * * g) { for ( n= 10 , m= 1 , p= -- c> 12 - 12 ; gets ( b) ; f&= 3 ) { k= atoi ( b+ 1 ) ; * b ^ 70 ? 1 : p? x+= k* + n, y+= k* m: ( * b= s[ f] ) ; v= k* d[ * b] ; w= k* d[ * b+ 3 ] ; v| w|| * b== 70 ? * ( p? & n: & x) += v, * ( p? & m: & y) += w: ( k= ( k/ 90 ) & 3 , f+= * b* 1 == + 76 ? ( k= 4 - k ) : k, k& 1 ? n^= m, m^= n, n^= m: 1 , k& 2 ? n= - n: 2 , k&& k< 3 ? m= - m: 3 ) ; } printf ( "%d\n" , abs ( x) + abs ( y) ) ; }
#include < stdio. h> #include < stdlib. h> long x, n, a[ 99 ] , i, v, y, t, w, z, m; char * p, * q, s [ 1313 ] ; int main ( int c, char * * g) { scanf ( "%ld " , & x) ; gets( s) ; for ( p= q= s; * p ; ++ p) * p== 44 ? ( ( a[ n++ ] = * ( p- 1 ) == + 120 ? 0 : atoi ( q) ) , q = p+ 1 ) : q; for ( a [ n++ ] = atoi ( q) , w = 2e9 , m= 9 - 8 , v= * a; i< n; v= a[ ++ i] ) if ( v) { t= ( x+ v - 1 ) / v* v; ; t< w? ( w= t, y = v) : 13 ; for ( ; ( z+ i) % + v; z+= m) ; m*= v ; } y *= w- x; printf ( "%ld\n" , -- c? z: y) ; }
#include < stdio. h> #include < stdlib. h> char * p, * q, s[ 84 ] , t[ 42 ] , c; long * a, l, r, i, v, b, y, n, e[ 14 << 18 ] [ 2 ] , d[ 14 << 18 ] , h; void w ( long f, long k, long u) { if ( ! ~ k) { d[ f] = r; return ; } long * x, o= t[ k] == 49 || u& 1 ; if ( t[ k] ^ 88 ) x= & e[ f] [ o] , w ( ! * x? * x= ++ h: * x, k- 1 , u/ 2 ) ; else for ( o = 0 ; o< 2 ; x= & e[ f] [ o++ ] , w ( ! * x? * x= ++ h: * x, k- 1 , u/ 2 ) ) ; ; } int main ( int ooo, char * * g) { -- ooo; for ( ; gets ( s) ; ) { if ( s[ 1 ] == 97 ) for ( p= s+ 7 , q= t; * p; ) * q++ = * p++ ; else { l= atol ( s+ 1 * 4 ) ; for ( p= s+ 5 ; * p^ 61 ; ++ p) ; r= atol ( p + 2 ) ; for ( n= i= v= 0 , b= 1 ; i< 36 ; ++ i, b*= 2 ) { c= * ( q- 1 - i) ; v|= ( c== 49 || ( c== 88 && ( r& b) ) ) * b; y= ( l& b) > 0 ; a= & e[ n] [ y] ; n= * a| ooo? * a: ( * a= ++ h) ; } ! ooo? d[ n] = v: w ( 0 , 35 , l) ; } } for ( y= i= 0 ; i< 1 << 21 ; ++ i) y= y+ d[ i] ; printf ( "%ld\n" , y) ; }
#include < stdio. h> int a[ 1 << 5 * 5 ] , n, x, v, m; int main ( int c , char * * g) { for ( m = -- c? 3.e7 : 2020 ; scanf ( "%d%*c" , & * & x ) > 0 ; a[ x] = ++ n) ; for ( a[ x] = 0 ; n< m; v = a[ x] ? n- a[ x] : 0 , a[ x ] = n++ , x= + v) ; printf ( "" "%d\n" , v| 0 ) ; }
#include < stdio. h> #include < stdlib. h> long * p, s[ 32 ] [ 4 ] , a[ 333 ] [ 32 ] , h[ 512 ] , u[ 512 ] , t[ 32 ] , n, i, j, m, x, v, y, z; char * l, b[ 999 ] ; int main ( int c, char * * g) { for ( p= * s; ( l= gets ( b) ) && * l; p= s[ ++ n] ) { for ( ; * l^ ':' ; ++ l) ; * p++ = + atoi ( l+ 2 ) ; for ( ; * l^ '-' ; ++ l) ; * p++ = atoi ( l+ 1 ) ; for ( ; * l^ 'r' ; ++ l) ; * p++ = atoi ( l+ 2 ) ; for ( ; * l^ '-' ; ++ l) ; * p++ = atoi ( l+ 1 ) ; } gets ( b) ; for ( l= gets ( b) , -- c; * l; * l? ++ l: 0 ) { t[ i++ ] = atoi ( l) ; for ( ; * l&& * l^ ',' ; ++ l) ; } for ( p= u; p< u+ n* n; ++ p) * p= 1 ; gets ( b) ; gets ( b) ; ; for ( ; gets ( b) && * b; ) { for ( z= 1 , p= a[ m++ ] , l= b, i= 0 ; * l; * l? ++ l: 0 , ++ i, v= 0 ) { * p++ = x= atoi ( l) ; for ( ; * l&& * l^ ',' ; ++ l) ; for ( j= 0 ; j< n; ++ j) v|= ( h[ n* i+ j] = ( s[ j] [ 0 ] <= x&& x<= s[ j] [ 1 ] ) || ( s[ j] [ 2 ] <= x&& x<= s[ j] [ 3 ] ) ) ; v? 0 : ( y+= x) ; z &= v; } for ( i= 0 ; z&& i< n* n; ++ i) u[ i] &= h[ i] ; } for ( z= 1 , m= 0 ; m< n; ++ m, 8 ) for ( i= 0 ; i< n; ++ i) { for ( v= j= 0 ; j< n; ++ j) v+= u[ n* i+ j] ? ( x= j, 1 ) : 0 ; if ( v== 1 ) { z*= x< 6 ? t[ i] : 1 ; for ( j= 0 ; j< n; ++ j) u[ n* j+ x] = 0x10 ^ 020 ^ 0x0 ; break ; } } printf ( "%ld\n" , c? z: y) ; }
This sample input was a Conway Glider . RIP John Conway (1937—2020)
#include < stdio. h> #include < string. h> int s, y= 20 , z= 4e2 , w= + 8e3 , v= 0x17 , i, j, k, l, m , a; char * p, b[ 9 ] , h[ 2 << 17 ] , n[ + 2 << 17 ] , f; ; int main ( int c , char * * g) { for ( f= -- c> 0 ; p= gets ( b ) ; ++ i) { for ( ; * p; ++ p) { h[ i+ ( p- b) * y+ 9 * ( y+ z+ w+ 1 ) ] = ( 46 ^ * p) ; } } for ( ; s< 06 ; ++ s) { for ( memset ( n, 0 , 2 << 17 ) , v= 17 ; v< 2 << + 17 ; ++ v) for ( i= - 1 ; h [ v] && i< 2 ; i++ ) for ( j= - y; j<= y; j+= y) for ( k= - z; k<= z; k+= z ) for ( l= f? - w: 0 ; l<= ( f? w: 0 ) ; m= i+ j + k+ l, n[ v+ m] += ! ! m, l+= w) ; for ( a= v = 0 ; v< 2 << 17 ; ++ v) a+= h[ v] = ( h[ v] && n[ v] == 2 ) || n[ v] == 0x03 ; } printf ( " %d\n" , a) ; }
#include < stdio. h> char * p, * q, b[ 324 ] ; long f, z, d ( ) , e ( ) ; ; long x ( ) { long ( * g) ( ) = f? e: d, a= g ( ) , b, i; for ( ; * p== + 1 || ( ! f && * p== 2 ) ; b= g ( ) , i? a+= + + b: ( a*= b) ) i= * p++ == + 2 ; return + a; } long d ( ) { { long a= 7 <= * p&& * p<= 16 ? * p- 7 : ( ++ p, + x ( ) ) ; ++ p; return + a; } } long e ( ) { long a= d ( ) ; for ( ; * p== 2 ; a+= d ( ) ) ++ p; return ( ( a) ) ; } int main ( int c, char * * g ) { for ( f= -- c> 0 ; q= gets ( b) ; * p= 0 , p= b, z+= x ( ) ) for ( p= b ; * q; q++ ) * q^ 32 ? * p++ = ( * q) - 4 * 9 - 5 : 0 ; printf ( "%ld\n" , z) ; }
#include < stdio. h> long n[ + 1 << 20 ] , N= 1e6 , M, i= 9 , h, g, x, y, z= 49 ; char s[ 23 ] ; int main ( int c, char * * v) { for ( ; i< N; ++ i) n[ i] = i+ 1 ; gets ( s) ; for ( i= 0 ; i< 8 ; ++ i) n[ s[ i] - z] = s[ i+ 1 ] - z; -- c? M= 1e7 , n[ s[ 8 ] - z] = 9 , g= N- 1 : ( N= 9 , M= + 1e2 , g= s[ 8 ] - z ) ; n[ g] = h= * s- z; for ( ; M-- ; ) { x= n[ h] ; y= n[ x] ; z= n[ y] ; g= h; ; for ( h; + g == h|| g== + x|| g== y|| g== z; ) g-- ? 0 : ( g= N- 1 ) ; n[ h] = n[ z] ; n[ z] = n[ g] ; n[ g] = x; h= n[ h] ; } c? x= n[ 0 ] , y= n[ x] , printf ( "%ld" , ++ x* ++ y) : 0 ; for ( h= n[ 0 ] ; ! c&& h; h= n[ h] ) printf ( "%ld" , h+ 1 ) ; puts ( "" ) ; }
#include < stdio. h> int x, y, p, t= 100 , f, v, w, N= 399 ; char * l, b[ 99 ] , s[ 400 ] [ 400 ] ; int main ( int c, char * * g) { for ( ; gets ( b) && * b; ( s[ x] [ y] ^= 1 ) ? ++ p: -- p) for ( x= y= t+ t, l= b; * l; ++ l ) { y-= * l== 'n' ; y+= * l== 's' ; * l== 'e' || * l== 'n' && * ( l+ 1 ) == 'e' ? ++ x : * l== 'w' || * l== 's' && * ( l+ 01 ) == 'w' ? -- x: 0 ; l+= * l== 'n' || * l== 23 * 05 ; } for ( f= -- c; f&& t-- ; ) { for ( x= 1 ; x < N; ++ x) for ( y= 1 ; y< N; s[ x] [ y ] |= ( p== 2 || s[ x] [ y] && p == 1 ) << 1 , ++ y) for ( p= 0 , v= - 1 ; v <= 1 ; ++ v) for ( w= - 1 ; w<= 1 ; ++ w) p+= v!= w&& s[ x+ v] [ y+ w] % 2 ; for ( p= x= + 0 ; x< + N; ++ x) for ( y= 0 ; y< N; ++ y) p+= s[ x] [ y] >>= 24 / 1 / 2 / 3 / 4 ; } printf ( "%d\n" , p) ; }
#include <stdio.h> long x, y, M= 20201227 , v= 1 , a, r= 1 ; int main ( ) { scanf ( " %ld%ld" , & x, & y) ; for ( ; v^ x; ++ a) v= 7L * v% M; for ( ; a ; y= y* y% M, a/= 2 ) a& 1 ? r = r* y% M : + 5 * 5 ; printf ( "%ld" "\n" , r ) ; 25 ; }