├── .gitignore ├── README.md ├── day01 ├── day01.c └── day01.py ├── day02 ├── day02.c └── day02.py ├── day03 ├── day03.c └── day03.py ├── day04 ├── day04.c ├── day04.py └── notes.txt ├── day05 ├── day05.c ├── day05.desc ├── day05.py └── notes.txt ├── day06 ├── day06.c └── day06.py ├── day07 ├── day07.c └── day07.py ├── day08 ├── day08.c └── day08.py ├── day09 ├── day09.c └── day09.py ├── day10 ├── day10.c ├── day10.desc └── day10.py ├── day11 ├── day11.c └── day11.py ├── day12 ├── day12.c ├── day12.desc └── day12.py ├── day13 ├── day13.c └── day13.py ├── day14 ├── day14.c └── day14.py ├── day15 ├── day15.c └── day15.py ├── day16 ├── day16.c └── day16.py ├── day17 ├── day17.c ├── day17.desc └── day17.py ├── day18 ├── day18.c └── day18.py ├── day19 ├── day19.c └── day19.py ├── day20 ├── day20.c └── day20.py ├── day21 ├── day21.c └── day21.py ├── day22 ├── day22.c └── day22.py ├── day23 ├── day23.c ├── day23.cpp └── day23.py ├── day24 ├── day24.c └── day24.py └── day25 ├── day25.c └── day25.py /.gitignore: -------------------------------------------------------------------------------- 1 | day*/*.in 2 | *.out 3 | scratch/ 4 | runall.sh 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advent of Code 2020 2 | 3 | ## Python 4 | 5 | The python solutions are my attempts to place on the leaderboard. Usually I 6 | butcher day 1 when solving day 2. Sometimes I restore / tidy the code before 7 | pushing to GitHub. Most of the time I won't. There's many better written python 8 | solutions on the internet. With these, I finished twelfth on the global 9 | leaderboard! 10 | 11 | ## C 12 | 13 | View these on my website: [http://arnavsastry.com/c\_art/](http://arnavsastry.com/c_art/). 14 | 15 | The C solutions are compiled with gcc on OSX: 16 | 17 | ``` 18 | $ gcc --version 19 | Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1 20 | Apple clang version 12.0.0 (clang-1200.0.26.2) 21 | Target: x86_64-apple-darwin19.6.0 22 | Thread model: posix 23 | InstalledDir: /Library/Developer/CommandLineTools/usr/bin 24 | ``` 25 | 26 | and compiled with `-Wall` (but not `-Werror`). In an effort to avoid the 27 | 5-character `stdin` token or format strings, I sometimes call `gets`, which is 28 | deprecated in C99, removed in later editions of the C standard, and horribly 29 | unsafe. Interestingly on OSX, using `gets` prints a runtime warning, but no 30 | compile time warnings. 31 | 32 | To compile a solution: 33 | ``` 34 | gcc -Wall -Werror -o day02.out day02.c 35 | ``` 36 | 37 | For the part 1 answer: 38 | ``` 39 | ./day02.out < day02.in 40 | ``` 41 | 42 | For the part 2 answer: 43 | ``` 44 | ./day02.out part2 < day02.in 45 | ``` 46 | 47 | In general, you can switch between part 1 and part 2 by passing any command line argument. 48 | 49 | I don't have many strict rules for myself, but I'm trying to hold myself to this standard: 50 | - Minimize spaces beyond the "hole" or between a type and identifier 51 | - No manually editing the input file (this is unlikely to budge) 52 | - No defines 53 | - No implicit int declarations 54 | - No external libraries (will definitely break if hashtables or hashing is strictly needed, but probably won't stray from [this list](https://gist.github.com/williewillus/07f534b706262ccc67119ddc70b2bd75)) 55 | 56 | I might break some of these "rules" as the days get harder. 57 | 58 | ## Notes on reducing token length 59 | 60 | This isn't code golf: the goal is to use as many short tokens as possible, even 61 | if you use a few bytes. Longer tokens are exponentially harder to work with 62 | than short tokens. Here are some tricks I've found that can save some tokens: 63 | 64 | String concatenation in C just works if you surround both parts with quotes, 65 | with no operator in between. This adds several characters though, so try and avoid it. 66 | 67 | ```c 68 | const char* s = "hello " 69 | "world"; 70 | ``` 71 | 72 | For loops are much more manageable than while loops 73 | ```c 74 | for(;;){ 75 | // stuff 76 | } 77 | ``` 78 | 79 | You can break up longer integer constants using math, but this will add more characters 80 | ```c 81 | assert(2020 == (99+2)*20) 82 | ``` 83 | 84 | Ternary statements are shorter than if statements, even if you don't have an else block. 85 | ```c 86 | if (x) { 87 | y = z; 88 | } 89 | ``` 90 | is equivalent to 91 | 92 | ```c 93 | x?y=z:0; 94 | ``` 95 | 96 | Use integer literals instead of character literals. It makes the code less 97 | readable, but was this ever readable? 98 | 99 | There are also many different tricks to pad with extra characters as needed. For example: 100 | ```c 101 | /* ADD 1 CHAR */ 102 | /* empty statements */ 103 | int x = 0;; 104 | 105 | /* unary plus */ 106 | int x = +0; 107 | 108 | /* ADD 2 CHAR */ 109 | /* extra scope blocks */ 110 | for (/**/) {{ }} 111 | 112 | /* trailing comment slashes */ 113 | int x = 0;// 114 | 115 | /* bonus parens */ 116 | int y = (5); 117 | 118 | /* ref and deref */ 119 | int x = *&y; 120 | 121 | /* identity for bools */ 122 | int x = !!x; 123 | 124 | /* ADD 4+ CHAR */ 125 | /**/ 126 | ``` 127 | 128 | It's best to re-arrange the code so the long tokens are at the beginning and 129 | ends. Unavoidable long tokens are usually format strings, input parsing 130 | functions like `scanf` and `gets`, and `printf`. 131 | 132 | # Megathread Comments 133 | 134 | - [Day 1](https://www.reddit.com/r/adventofcode/comments/k4e4lm/2020_day_1_solutions/geash89) 135 | - [Day 2](https://www.reddit.com/r/adventofcode/comments/k52psu/2020_day_02_solutions/gecftlc) 136 | - [Day 3](https://www.reddit.com/r/adventofcode/comments/k5qsrk/2020_day_03_solutions/gehy1od) 137 | - [Day 4](https://www.reddit.com/r/adventofcode/comments/k6e8sw/2020_day_04_solutions/gelx4er) 138 | - [Day 5](https://www.reddit.com/r/adventofcode/comments/k71h6r/2020_day_05_solutions/geq8cj8) 139 | - [Day 6](https://www.reddit.com/r/adventofcode/comments/k7ndux/2020_day_06_solutions/gesc8cy) 140 | - [Day 7](https://www.reddit.com/r/adventofcode/comments/k8a31f/2020_day_07_solutions/gex8luw) 141 | - [Day 8](https://www.reddit.com/r/adventofcode/comments/k8xw8h/2020_day_08_solutions/gf15x3p) 142 | - [Day 9](https://www.reddit.com/r/adventofcode/comments/k9lfwj/2020_day_09_solutions/gf59qvb) 143 | - [Day a](https://www.reddit.com/r/adventofcode/comments/ka8z8x/2020_day_10_solutions/gf9adw7) 144 | - [Day b](https://www.reddit.com/r/adventofcode/comments/kaw6oz/2020_day_11_solutions/gfddfic) 145 | - [Day c](https://www.reddit.com/r/adventofcode/comments/kbj5me/2020_day_12_solutions/gfhy27d) 146 | - [Day d](https://www.reddit.com/r/adventofcode/comments/kc4njx/2020_day_13_solutions/gfnrzfr) 147 | - [Day e](https://www.reddit.com/r/adventofcode/comments/kcr1ct/2020_day_14_solutions/gfsbmlg) 148 | - [Day f](https://www.reddit.com/r/adventofcode/comments/kdf85p/2020_day_15_solutions/gfwbyda) 149 | -------------------------------------------------------------------------------- /day01/day01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // AOC DAY NUMBER // 5 | /**/const int M=2020 6 | ;char h[M];int main( 7 | int c,char **v){int 8 | x,i,j,a= 0;while( 9 | scanf (" %d",& 10 | x)>0)if( x 2 | #include 3 | #include 4 | 5 | // AOC DAY NUMBER // 6 | char*s=" %s%s%s";int 7 | main(int g,char**v){ 8 | char c[99],a[9],b[9] 9 | ,*k;/**/int y=0,z=0; 10 | while( scanf(s 11 | ,a,b ,c)>0 12 | ){ int u, h,x 13 | =strcspn(a,"-" ); 14 | ;a[x]=0;u=atoi (a 15 | );h=atoi(a+ x+1) 16 | ;int f=0; for (k 17 | =c;*k; ++k){f+=* 18 | k== *b;}y+=u<=f 19 | &&f <=h 20 | ;z +=(c 21 | [--u]/*DAY2*/==*b)^( 22 | c[--h]==*b);}printf( 23 | "%d\n", --g?+z:+y);} 24 | -------------------------------------------------------------------------------- /day02/day02.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | def main(): 10 | fin = open("day02.in", "r") 11 | lines = [line.strip() for line in fin.readlines() if line.strip()] 12 | p1, p2 = 0, 0 13 | for line in lines: 14 | a, b, c = line.split() 15 | b = b[0] 16 | lo, hi = map(int, a.split("-")) 17 | p1 += lo <= c.count(b) <= hi 18 | p2 += sum(c[x - 1] == b for x in (lo, hi)) == 1 19 | print(p1) 20 | print(p2) 21 | 22 | if __name__ == "__main__": 23 | main() 24 | -------------------------------------------------------------------------------- /day03/day03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // AOC DAY NUMBER // 4 | char g[500][500];int 5 | main(int c,char**v){ 6 | ;long n=0,a=1,m=0,i; 7 | ;while (gets( 8 | g[n++ ])); 9 | ;int o[]={ 1,3 10 | ,1,1, 1,5,1, 7,2 11 | ,1};while(g[0 ][++ 12 | m]);for(i= (--c, 13 | 0);i<10; i+=2){{ 14 | int x=0,y= 0,t=0 15 | ,d=*(o+i),r= *(o+ 16 | i+1 );for( ;x< 17 | +n;x +=d,y =(y 18 | +r)%m )t+= 19 | g[x][y] =='#';a 20 | *=t;if(!c){break;}}} 21 | ;printf("%ld\n",a);} 22 | -------------------------------------------------------------------------------- /day03/day03.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | def main(): 10 | fin = open("day03.in", "r") 11 | lines = [line.strip() for line in fin.readlines() if line.strip()] 12 | 13 | n, m = len(lines), len(lines[0]) 14 | moves = [(1, 3), (1, 1), (1, 5), (1, 7), (2, 1)] 15 | ans = 1 16 | for dx, dy in moves: 17 | x, y = 0, 0 18 | cur = 0 19 | while x < n: 20 | cur += lines[x][y] == '#' 21 | x += dx 22 | y += dy 23 | y %= m 24 | 25 | ans *= cur 26 | # uncomment the following line for part 1 solution 27 | # break 28 | 29 | print(ans) 30 | 31 | if __name__ == "__main__": 32 | main() 33 | -------------------------------------------------------------------------------- /day04/day04.c: -------------------------------------------------------------------------------- 1 | #include/* 2 + 2 =*/ 2 | #include/* 2 * 2 =*/ 3 | 4 | // AOC 2020 !! DAY NUMBER // 5 | int main(int r,char**v){char b 6 | [99],d[30][10],*l=b,*k;int h,p 7 | ,i,a=0,c=0,f[]={27,16,28,15,11 8 | ,6,25},g[]={00,20,13,29,18,17, 9 | 20};for(;l;){for(h=0;h<30;++h) 10 | *d[h]=0;for(;(l= gets(b)) 11 | &&l&&*l;)for(k =b;*k;k 12 | =*k?k+1:k){h= *k^*(k+ 13 | 1);k+=4;for( p=0;+*k 14 | &&*k^32;)d[ h] [p++]=* 15 | (k++);d[h] [p] =0;}for 16 | (i=0,h=0; l&&i <7;++i) 17 | h+=*d[f[ i]]>0 ;if(h== 18 | 7){++a; i=1919 ;p=atoi 19 | (d[27] )-i;h-= 0 2 | 3 | // AOC DAY NUMBER // 4 | int a=0,t=1,x;char b 5 | [32], h[1 6 | <<10 ],*k;int main 7 | (int c,char**v){for 8 | (-- c;gets 9 | (b) ;)for(x=0 ,k= 10 | b;*k;x=2*x|!(*k &4 11 | ),h[x]=5,a=a>x? +a 12 | :x,k ++);for(; !h 13 | [t- 1]|h[t]| !h 14 | [t+1 ];)++t; //V 15 | printf ("%d" 16 | /*CD*/"\n",c?t:+a);} 17 | -------------------------------------------------------------------------------- /day05/day05.desc: -------------------------------------------------------------------------------- 1 | The shape of this 5 was inspired by I Saw the Figure 5 in Gold by Charles Demuth. 2 | -------------------------------------------------------------------------------- /day05/day05.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | def main(): 10 | fin = open("day05.in", "r") 11 | lines = [line.strip() for line in fin.readlines() if line.strip()] 12 | ans = 0 13 | s = set() 14 | for line in lines: 15 | lo = 0 16 | hi = 1 << 7 17 | for c in line[:7]: 18 | mid = (lo + hi) // 2 19 | if c == "F": 20 | hi = mid 21 | elif c == "B": 22 | lo = mid 23 | 24 | l2, r2 = 0, 8 25 | for c in line[7:]: 26 | mid = (l2 + r2) // 2 27 | if c == "L": 28 | r2 = mid 29 | else: 30 | l2 = mid 31 | 32 | x = lo * 8 + l2 33 | ans = max(ans, x) 34 | s.add(x) 35 | 36 | print(ans) 37 | print(set(range(1000)) - s) 38 | 39 | 40 | if __name__ == "__main__": 41 | main() 42 | -------------------------------------------------------------------------------- /day05/notes.txt: -------------------------------------------------------------------------------- 1 | This 5 was inspired by the painting, I Saw the Figure 5 in Gold. 2 | 3 | https://en.wikipedia.org/wiki/I_Saw_the_Figure_5_in_Gold 4 | 5 | Part 1: 6 | ```c 7 | for(x=0,k=b;*k;x=2*x|!(*k&4),a=a>x?a:x,++k); 8 | ``` 9 | -------------------------------------------------------------------------------- /day06/day06.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // AOC DAY NUMBER // 5 | int main(int c,char* 6 | *v){int t,p,q; 7 | char b[66] 8 | ,h[ 66],*k ,*l= 9 | b,* e=h+26;for(p=q 10 | =0 ;l; ){for(k=h 11 | ;k 0;q+=*k==t;}}--c; 19 | printf("%d",c?q:p);} 20 | -------------------------------------------------------------------------------- /day06/day06.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | def main(): 10 | fin = open("day06.in", "r") 11 | blocks = fin.read().split("\n\n") 12 | part1 = 0 13 | for block in blocks: 14 | c = Counter(block) 15 | part1 += len(c) 16 | if '\n' in c: 17 | part1 -= 1 18 | 19 | part2 = 0 20 | for block in blocks: 21 | x = len(block.split()) 22 | c = Counter(block) 23 | part2 += sum(v == x for _, v in c.items()) 24 | if c['\n'] == x: 25 | part2 -= 1 26 | 27 | print(part1) 28 | print(part2) 29 | 30 | 31 | if __name__ == "__main__": 32 | main() 33 | -------------------------------------------------------------------------------- /day07/day07.c: -------------------------------------------------------------------------------- 1 | #include/* do you */ 2 | #include/* feel */ 3 | #include/* lucky? */ 4 | 5 | // AOC 2020 !! DAY NUMBER // 6 | int*w,p,q,n,m,x[777][7],y[777] 7 | [7],z[777][77];char*k,c[777][7 8 | ][77],d[777][7][7][77],b[777]; 9 | int h(int v){if(*x[v]<0)return 10 | 0; int 11 | g=1 ;for 12 | (*x [v] 13 | =-1; b[v]--;)g+=h( z[v 14 | ][b[ v]]);return g;} int 15 | f(int v){int g=1,i=0 ;for 16 | (;x[v][i];++i)g+=x[ v][i] 17 | *f(y[v][i]);return g;}int 18 | main(int g,char** v){for( 19 | ;gets(b);++n)for (sscanf( 20 | b,"%s%s%*s%*s" "%n",*c[n 21 | ],c[n][1],&p ),m=0;b[p] 22 | ;++m,p+=1*q) sscanf(b+p, 23 | "%0d%s%s%*s" "%n",x[n]+m, 24 | *d[n][m],d[ n][m][1],&q) 25 | ;memset(b,+ 0,777);for(p= 26 | 0;p 2 | #include 3 | #include 4 | 5 | // AOC DAY NUMBER // 6 | int x[888],e,i,g,h,t 7 | ,n;char b[888][9],s[ 8 | 888];int main(int c, 9 | char**v ){for(; 10 | gets( b[n]) 11 | ;x[ n+!!0]= atoi 12 | (b [n]+8/2), ++n 13 | ); for(i=-1;! t&& 14 | i< n;e>=n?t =g: 15 | !~i ?h=g:8 ,++i 16 | ){for( memset 17 | (s,0, 888), 18 | e=g= 0;e< +n&& 19 | !s [e];++e) {s[ 20 | e] =1;b[e][0 ]== 21 | 97 ?g+=x[e]: i== 22 | e^b [e][0]== 'j'? 23 | e+= x[e] 24 | -1:8;} }printf 25 | ("%d\n",~~--c?t:h);} 26 | -------------------------------------------------------------------------------- /day08/day08.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | def run(prog): 10 | pc = 0 11 | acc = 0 12 | seen = set() 13 | while pc < len(prog): 14 | if pc in seen: 15 | # part 1, return acc here 16 | return None 17 | 18 | seen.add(pc) 19 | op, x = prog[pc] 20 | if op == "acc": 21 | acc += x 22 | pc += 1 23 | elif op == "jmp": 24 | pc += x 25 | else: 26 | pc += 1 27 | 28 | return acc 29 | 30 | 31 | def main(): 32 | fin = open("day08.in", "r") 33 | prog = [line.strip().split() for line in fin.readlines() if line.strip()] 34 | prog = [(op, int(x)) for op, x in prog] 35 | 36 | for i in range(len(prog)): 37 | if prog[i][0] == "acc": 38 | continue 39 | new_op = "jmp" if prog[i][0] == "nop" else "nop" 40 | new_prog = prog[:i] + [(new_op, prog[i][1])] + prog[i+1:] 41 | x = run(new_prog) 42 | if x is not None: 43 | print(x) 44 | 45 | if __name__ == "__main__": 46 | main() 47 | -------------------------------------------------------------------------------- /day09/day09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // AOC DAY NUMBER // 4 | long*p,*q,*r,a[9999] 5 | ,n,v,z;;int main(int 6 | c,char**IX){for(p=a; 7 | scanf ("%ld", 8 | p++ )>0; 9 | ++ n);for (v=1 10 | ,p =a+25;v &&p< 11 | a+ +n;++p) for( 12 | q= p-25,v= 0;q< 13 | p; ++q)for (r=q 14 | +1 ;r n?n=*r: 9,*r 24 | 2 | #include/* we hex */ 3 | 4 | // ADVENT OF CODE 2020 DAY // 5 | long*p,*q,y,t,e,n,a[110],b[110 6 | ];int main( int c,//X 7 | char**v){ for(n= 8 | 2,p=a+1; scanf("%ld" ,p)> 9 | 0;++p,++n);for(e=0;e* 11 | (q+1))*q^=* (q+1 12 | )^=*q^=* (q +1); 13 | *p=*(p- 1)+3;for(q=a ;q

sorting algorithm. 2 | -------------------------------------------------------------------------------- /day10/day10.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | def main(): 10 | fin = open("day10.in", "r") 11 | xs = [int(line.strip()) for line in fin.readlines() if line.strip()] 12 | xs.sort() 13 | xs = [0] + xs + [xs[-1] + 3] 14 | last = 0 15 | a, b = 0, 0 16 | for x in xs: 17 | assert x - last <= 3 18 | if x - last == 1: 19 | a += 1 20 | elif x - last == 3: 21 | b += 1 22 | last = x 23 | print("part 1", a, b, a * b) 24 | 25 | dp = [1] 26 | for i in range(1, len(xs)): 27 | ans = 0 28 | for j in range(i): 29 | if xs[j] + 3 >= xs[i]: 30 | ans += dp[j] 31 | dp.append(ans) 32 | 33 | print("part 2", dp[-1]) 34 | 35 | if __name__ == "__main__": 36 | main() 37 | -------------------------------------------------------------------------------- /day11/day11.c: -------------------------------------------------------------------------------- 1 | #include/* eleven? */ 2 | 3 | // ADVENT OF CODE 2020 DAY // 4 | char*p,*q,n,m,a[121][121],b[11 5 | *11][121],(*l)[121],(*r)[121]; 6 | int f,t,y,o,i,j,v,w,g,h,k=043; 7 | int main(int c,char**d){// 8 | ;for( n=+1;gets(b[n++]+1);); 9 | for(p =b[1]+(m=1);*p;++p,++m 10 | );for (i=0;i=4+!!c?76 :k:* 19 | p==76 ?!o?k:76:*p),f |=*q 20 | ^+*p, ++j)for(o=0, v=-1; 21 | v<=1* +1;++v) 22 | for(w =-1 ;w<2;o+=( 23 | v|w++)&&l[g][h]==k)if(g=i+v,h= 24 | j+w,(v|w)&&c)for(;l[g][h]==46; 25 | g+=v,h+=w);}printf("%d\n",y);} 26 | -------------------------------------------------------------------------------- /day11/day11.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | def h(grid): 10 | return "".join("".join(row) for row in grid) 11 | 12 | 13 | def step1(grid): 14 | nxt = [['.' for _ in row] for row in grid] 15 | 16 | for i, row in enumerate(grid): 17 | for j, c in enumerate(row): 18 | ct = Counter(grid[i + dx][j + dy] for dx in range(-1, 2) for dy in range(-1, 2) if 0 <= i + dx < len(grid) and 0 <= j + dy < len(grid[i]) and (dx, dy) != (0, 0)) 19 | if c == 'L' and ct['#'] == 0: 20 | nxt[i][j] = '#' 21 | elif c == '#' and ct['#'] >= 4: 22 | nxt[i][j] = 'L' 23 | else: 24 | nxt[i][j] = grid[i][j] 25 | return nxt 26 | 27 | 28 | def step2(grid): 29 | nxt = [['.' for _ in row] for row in grid] 30 | n,m = len(grid), len(grid[0]) 31 | 32 | for i, row in enumerate(grid): 33 | for j, c in enumerate(row): 34 | occ = 0 35 | for dx in range(-1, 2): 36 | for dy in range(-1, 2): 37 | if (dx, dy) == (0, 0): 38 | continue 39 | 40 | nx = i + dx 41 | ny = j + dy 42 | while 0 <= nx < n and 0 <= ny < m and grid[nx][ny] == '.': 43 | nx += dx 44 | ny += dy 45 | 46 | if 0 <= nx < n and 0 <= ny < m: 47 | occ += grid[nx][ny] == '#' 48 | 49 | 50 | if c == 'L' and occ == 0: 51 | nxt[i][j] = '#' 52 | elif c == '#' and occ >= 5: 53 | nxt[i][j] = 'L' 54 | else: 55 | nxt[i][j] = grid[i][j] 56 | return nxt 57 | 58 | 59 | def main(): 60 | grid = [list(line.strip()) for line in sys.stdin if line.strip()] 61 | 62 | while True: 63 | nxt = step2(grid) 64 | if h(nxt) == h(grid): 65 | break 66 | 67 | grid = nxt 68 | 69 | print(h(grid).count("#")) 70 | 71 | 72 | if __name__ == "__main__": 73 | main() 74 | -------------------------------------------------------------------------------- /day12/day12.c: -------------------------------------------------------------------------------- 1 | #include/* twelve */ 2 | #include/* is 0xc */ 3 | 4 | // ADVENT OF CODE 2020 DAY // 5 | int n,m,x,y,f,k,v,w,p; char b[ 6 | 912],d[212]={[69]=+1,[86]=-1,[ 7 | 87]=-1,[81]=1},*s="ESWN",e;int 8 | main(int c,char **g){for(n=10, 9 | m=1,p=--c>12-12 ;gets(b);f&=3) 10 | {k=atoi(b+1);*b ^70?1:p?x+=k*+ 11 | n,y+=k*m:(*b=s[ f]);v=k*d[*b// 12 | ];w=k*d[*b+3]; v|w||*b==70?* 13 | (p?&n:&x)+=v,* (p?&m:&y)+=w: 14 | (k=(k/90)&3 ,f+=*b*1==+ 15 | 76?(k=4-k ):k,k&1?n 16 | ^=m,m^=n, n^=m:1,k& 17 | 2?n=-n:2, k&&k<3?m= 18 | -m:3);} printf( 19 | "%d\n" ,abs(x 20 | )+abs( y));} 21 | -------------------------------------------------------------------------------- /day12/day12.desc: -------------------------------------------------------------------------------- 1 | When this code is given as a Rorschach test, subjects have responded with “guitar”, “government building”, and “church”. The goal was “cake”. 2 | -------------------------------------------------------------------------------- /day12/day12.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | moves = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} 10 | 11 | s = "NESW" 12 | 13 | def part2(): 14 | lines = [line.strip() for line in sys.stdin if line.strip()] 15 | 16 | x, y = 0, 0 17 | wx, wy = 10, 1 18 | f = 1 19 | for line in lines: 20 | d = line[0] 21 | k = int(line[1:]) 22 | 23 | if d in moves: 24 | dx, dy = moves[d] 25 | wx += k * dx 26 | wy += k * dy 27 | else: 28 | dc = (k // 90) 29 | dc %= 4 30 | if d == 'L': 31 | dc = 4 - dc 32 | if d in 'LR': 33 | for _ in range(dc): 34 | wx, wy = wy, -wx 35 | else: 36 | print("moving", s[f], moves[s[f]]) 37 | x += k * wx 38 | y += k * wy 39 | 40 | print("x y", x, y, "wx wy", wx, wy) 41 | 42 | 43 | def main(): 44 | lines = [line.strip() for line in sys.stdin if line.strip()] 45 | 46 | x, y = 0, 0 47 | f = 1 48 | for line in lines: 49 | d = line[0] 50 | print(line[1:]) 51 | k = int(line[1:]) 52 | 53 | if d in moves: 54 | dx, dy = moves[d] 55 | x += k * dx 56 | y += k * dy 57 | else: 58 | dc = (k // 90) 59 | if d == 'L': 60 | f = (f - dc) % 4 61 | elif d == 'R': 62 | f = (f + dc) % 4 63 | else: 64 | print("moving", s[f], moves[s[f]]) 65 | dx, dy = moves[s[f]] 66 | x += k * dx 67 | y += k * dy 68 | 69 | print(x, y) 70 | 71 | print(x, y, abs(x) + abs(y)) 72 | 73 | 74 | if __name__ == "__main__": 75 | part2() 76 | -------------------------------------------------------------------------------- /day13/day13.c: -------------------------------------------------------------------------------- 1 | #include/*1101*/ 2 | #include/*015*/ 3 | 4 | // AOC 2020 DAY NUMBER // 5 | long x,n,a[99],i,v,y,t,w, 6 | z,m;char*p,*q,s [1313]; 7 | int main(int c, char**g 8 | ){scanf("%ld ",& x);gets 9 | (s);for(p=q=s;*p ;++p)*p 10 | ==44?((a[n++]=*( p-1)==+ 11 | 120?0:atoi(q)),q =p+1):q 12 | ;for(a [ n++]=// 13 | atoi( q),w =2e9,m= 14 | 9-8, v=*a;i 2 | #include/* = fourteen */ 3 | 4 | //ADVENT OF CODE 2020 DAY NUMBER// 5 | char*p,*q,s[84],t[42],c;long*a,l,r, 6 | i,v,b,y,n,e[14<<18][2],d[14<<18],h; 7 | void w(long f,long k,long u){if(!~k 8 | ){d[f]=r; return;}long* 9 | x,o=t[k] == 49||u&1;if 10 | (t[k]^ 88)x=&e[f] [o],w(!* 11 | x?*x= ++h:*x,k-1,u/ 2);else 12 | for(o =0;o<2;x=&e[f] [o++], 13 | w(!* x?*x= 14 | ++h: *x,k-1,u/2));;}int main(int 15 | ooo, char**g){--ooo;for(;gets(// 16 | s);) {if(s[1]==97)for(p=s+7,q=t 17 | ;*p;) *q++=*p++;else{l= atol(s 18 | +1*4); for(p=s+5;*p^61; ++p);r 19 | =atol(p +2);for(n= i=v=0, 20 | b=1;i<36; ++i,b*=2 21 | ){c=*(q-1-i) ;v|=(c==49|| 22 | (c==88&&(r&b)))*b;y=(l&b)>0;a=&e[n] 23 | [y];n=*a|ooo?/*E*/*a:(*a=++h);}!ooo 24 | ?d[n]=v:w(0,35,l);}}for(y=i=0;i<1<< 25 | 21;++i)y=y+d[i];printf("%ld\n",y);} 26 | -------------------------------------------------------------------------------- /day14/day14.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | 10 | def part1(): 11 | lines = [line.strip() for line in sys.stdin if line.strip()] 12 | m = defaultdict(int) 13 | for line in lines: 14 | a, b = line.split(" = ") 15 | if a == "mask": 16 | mask = b 17 | else: 18 | a = int(a[4:-1]) 19 | b = int(b) 20 | v = 0 21 | for i in range(36): 22 | c = mask[-i-1] 23 | if c == '1' or (c == 'X' and (b & (1 << i)) > 0): 24 | v |= (1 << i) 25 | 26 | m[a] = v 27 | 28 | print(sum(m.values())) 29 | 30 | 31 | def part2(): 32 | lines = [line.strip() for line in sys.stdin if line.strip()] 33 | m = defaultdict(int) 34 | for line in lines: 35 | a, b = line.split(" = ") 36 | if a == "mask": 37 | mask = b 38 | else: 39 | a = int(a[4:-1]) 40 | b = int(b) 41 | v = 0 42 | extra = [] 43 | for i in range(36): 44 | c = mask[-i-1] 45 | if c == 'X': 46 | extra.append(1 << i) 47 | elif c == '1' or (a & (1 << i)) > 0: 48 | v += (1 << i) 49 | 50 | for k in range(len(extra) + 1): 51 | for c in itertools.combinations(extra, k): 52 | s = sum(c) 53 | m[v + s] = b 54 | 55 | print(sum(m.values())) 56 | 57 | 58 | part2() 59 | -------------------------------------------------------------------------------- /day15/day15.c: -------------------------------------------------------------------------------- 1 | #include/*PRESS*/ 2 | 3 | int a[1<<5 4 | *5],n,x,v,m;int 5 | main( int c 6 | ,char* *g){ 7 | for(m 8 | =--c? 9 | 3.e7: 10 | 2020; 11 | scanf("%d%*c",&*&x 12 | )>0;a[x]=++n);for( 13 | a[x]=0 14 | ;n 2 | #include/* its fields encoded in hexadecimal or binary! */ 3 | 4 | // ADVENT OF CODE 2020 DAY 16 || TICKET TRANSLATION // 5 | long*p,s[32][4],a[333][32],h[512],u[512],t[32],n,i,j,m,x,v,y,z;char*l, 6 | b[999];int main(int c,char**g){for(p=*s;(l=gets(b))&&*l;p=s[++n]){for( 7 | ;*l^':' ;++l);*p++=+ atoi(l+2) 8 | ;for(/* DATE:: 0x89 */;*l^'-';/* PLATFORM:: 00111011 */++l);*p 9 | ++=atoi (l+1);for(;* l^'r';++l 10 | );*p++=atoi(l+2);for(;*l^'-';++l);*p++=atoi(l+1);}gets(b);for(l=gets(b 11 | ),--c;*l;*l?++l:0){t[i++]=atoi(l);for(;*l&&*l^',';++l);}for(p=u;p 2 | #include/* on samples! */ 3 | 4 | // AOC 2020 DAY 17 SAMPLE INPUT // 5 | 6 | int s,y=20 7 | ,z=4e2,w=+ 8 | 8e3,v=0x17 9 | ,i,j,k,l,m 10 | ,a;char*p, 11 | b[9],h[2<< 12 | 17],n[+2<< 13 | 17],f;;int 14 | 15 | main(int c 16 | ,char**g){ 17 | for(f=--c> 18 | 0;p=gets(b 19 | );++i){for 20 | (;*p;++p){ 21 | h[i+(p-b)* 22 | y+9*(y+z+w 23 | 24 | +1)]=(46^* p);}}for(; s<06;++s){ 25 | for(memset (n,0,2<<17 ),v=17;v<2 26 | <<+17;++v) for(i=-1;h [v]&&i<2;i 27 | ++)for(j=- y;j<=y;j+= y)for(k=-z 28 | ;k<=z;k+=z /* XVII */ )for(l=f?- 29 | w:0;l<=(f? w:0);m=i+j +k+l,n[v+m 30 | ]+=!!m,l+= w);for(a=v =0;v<2<<17 31 | ;++v)a+=h[ v]=(h[v]&& n[v]==2)|| 32 | n[v]==0x03 ;}printf(" %d\n",a);} 33 | -------------------------------------------------------------------------------- /day17/day17.desc: -------------------------------------------------------------------------------- 1 | This sample input was a Conway Glider. RIP John Conway (1937—2020) 2 | -------------------------------------------------------------------------------- /day17/day17.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | def step(state): 10 | have = set(state) 11 | new_state = [] 12 | 13 | # just uh 14 | # remove all the w parts for a sol to part 1 15 | poss = set([(x + dx, y + dy, z + dz, w + dw) for x, y, z, w in state for dx in range(-1, 2) for dy in range(-1, 2) for dz in range(-1, 2) for dw in range(-1, 2)]) 16 | for nx, ny, nz, nw in poss: 17 | ct = sum((nx + dx, ny + dy, nz + dz, nw + dw) in have for dx in range(-1, 2) for dy in range(-1, 2) for dz in range(-1, 2) for dw in range(-1, 2)) 18 | ct -= (nx, ny, nz, nw) in have 19 | if ct == 3 or (ct == 2 and (nx, ny, nz, nw) in have): 20 | new_state.append((nx, ny, nz, nw)) 21 | 22 | return new_state 23 | 24 | 25 | def main(): 26 | grid = [list(line.strip()) for line in sys.stdin if line.strip()] 27 | state = [] 28 | for i, row in enumerate(grid): 29 | for j, cell in enumerate(row): 30 | if cell == '#': 31 | state.append((i, j, 0, 0)) 32 | 33 | for _ in range(6): 34 | state = step(state) 35 | 36 | print(len(state)) 37 | 38 | main() 39 | -------------------------------------------------------------------------------- /day18/day18.c: -------------------------------------------------------------------------------- 1 | #include/*math=math*/ 2 | 3 | // AOC 2020 DAY 18 SYMBOLS // 4 | char*p,*q,b[324];long f,z,d(), 5 | e();; long x(){long(*g)()=f 6 | ?e:d, a=g(),b,i;for(;*p==+1 7 | ||(!f &&*p==2);b=g(),i?a+=+ 8 | + b:(a*=b))i=*p++== 9 | + 2;return +a;}long 10 | d(){{ long a=7<=*p&&*p<=16? 11 | *p-7: (++p,+x());++p;return 12 | +a;}} long e (){ long 13 | a=d();for(;*p==2 ; a+=d( 14 | ))++p;return ((a) );}int 15 | main(int c,char**g ){for(f 16 | =--c>0;q=gets(b); *p=0,p 17 | =b,z+=x())for(p= b ;*q;q 18 | ++)*q^32?*p++=( *q) -4*9 19 | -5:0;printf("%ld\n",z);/*18*/} 20 | -------------------------------------------------------------------------------- /day18/day18.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | def parse(eqn, is_part_2=False): 10 | eqn = eqn.replace("(", "( ") 11 | eqn = eqn.replace(")", " )") 12 | 13 | # accidentally made this right associative, so reverse all the tokens 14 | # and flip parens 15 | tokens = eqn.split()[::-1] 16 | stk = [] 17 | ops = [] 18 | for token in tokens: 19 | if token == '(': 20 | while ops[-1] != ")": 21 | stk.append(ops.pop()) 22 | ops.pop() 23 | elif token == "*": 24 | # handle precedence 25 | while is_part_2 and ops and ops[-1] == "+": 26 | stk.append(ops.pop()) 27 | ops.append(token) 28 | elif token in ")+": 29 | ops.append(token) 30 | else: 31 | stk.append(int(token)) 32 | 33 | while ops: 34 | stk.append(ops.pop()) 35 | 36 | cur = [] 37 | for val in stk: 38 | if val == "+": 39 | x = cur[-1] + cur[-2] 40 | cur.pop() 41 | cur.pop() 42 | cur.append(x) 43 | elif val == "*": 44 | x = cur[-1] * cur[-2] 45 | cur.pop() 46 | cur.pop() 47 | cur.append(x) 48 | else: 49 | cur.append(val) 50 | 51 | return cur[0] 52 | 53 | 54 | def main(): 55 | lines = [line.strip() for line in sys.stdin if line.strip()] 56 | part1 = 0 57 | part2 = 0 58 | for line in lines: 59 | part1 += parse(line, False) 60 | part2 += parse(line, True) 61 | 62 | print("part 1", part1) 63 | print("part 2", part2) 64 | 65 | main() 66 | -------------------------------------------------------------------------------- /day19/day19.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int n,r[191][6],i,d,a,t[99][99][191],e; 6 | char*p,b[99],v[99][99][191]; 7 | 8 | char s(int u, int l, int h) { 9 | int*q=r[u],*x=&t[l][h][u]; 10 | if(l==h) 11 | return !q[0]&&b[l]==q[1]||!q[2]&&b[l]==q[3]; 12 | 13 | char*o=&v[l][h][u]; 14 | *x^e?*o=0:0; 15 | for(int d=0;*x^e&&d<5;d+=2) 16 | for(int j=l;!*o&&q[d]>0&&q[d+1]>0&&ji?n:i+1){ 25 | i = atoi(p); 26 | for(;*p^32;++p); 27 | if (*++p==34) 28 | r[i][1]=*++p; 29 | else{ 30 | for(d=0;*p;++d){ 31 | r[i][d]=atoi(p); 32 | for(;*p&&*p^32;++p); 33 | *p&&*++p==124?p+=2,d=1:0; 34 | } 35 | } 36 | } 37 | for(i=0;i 10: 22 | return 23 | 24 | if '"' in rules[k]: 25 | yield rules[k][1:-1] 26 | else: 27 | for sub in rules[k].split(" | "): 28 | for p in itertools.product(*[solve(int(s), d + 1) for s in sub.split()]): 29 | yield "".join(p) 30 | 31 | valid = set(solve(0)) 32 | part1 = sum(s in valid for s in data) 33 | 34 | first = set(solve(42)) 35 | second = set(solve(31)) 36 | part2 = 0 37 | k = [len(x) for x in first][0] 38 | assert(all(len(x) == k for x in first | second)) 39 | for s in data: 40 | if len(s) % k != 0: 41 | continue 42 | 43 | dp1 = [s[i:i+k] in first for i in range(0, len(s), k)] 44 | dp2 = [s[i:i+k] in second for i in range(0, len(s), k)] 45 | 46 | part2 += any(all(dp1[:i]) and all(dp2[i:]) and i > (len(dp1) - i) for i in range(len(dp1))) 47 | 48 | print("part 1", part1) 49 | print("part 2", part2) 50 | 51 | main() 52 | -------------------------------------------------------------------------------- /day20/day20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long s[150],l[15][15][2],n,k,i,j,S=10,d,y,F[]={1,8,6},M[][8]={{18},{0,5,6,11,12,17,18,19},{1,4,7,10,13,16}},x,m,z,t; 5 | char(*p)[15],b[20],e[150][8][15][15],f[8][200][200],u[150]; 6 | long dfs(int r,int c){ 7 | long*h,o=0,g=0; 8 | if(c==d) 9 | o=dfs(r+1,0); 10 | else if(r==d) 11 | o=1; 12 | else{ 13 | for(;!o&&g 0: 79 | top = "".join(opt[0]) 80 | before = "".join(stitched[K * r - 1][K*c:K*c+K]) 81 | can_place &= top == before 82 | # print("top fail", top, before) 83 | if c > 0: 84 | left = "".join(row[0] for row in opt) 85 | before = "".join(stitched[row][K * c - 1] for row in range(K * r, K * r + K)) 86 | can_place &= left == before 87 | # print("left fail", left, before) 88 | if can_place and dfs(r, c + 1): 89 | return True 90 | 91 | placed.remove(tile_id) 92 | ids[r][c] = -1 93 | 94 | return False 95 | dfs(0, 0) 96 | print("part1", ids[0][0] * ids[-1][0] * ids[0][-1] * ids[-1][-1]) 97 | out = [[]] 98 | for i in range(N * K): 99 | for j in range(N * K): 100 | if i % K in (0, K - 1) or j % K in (0, K - 1): 101 | continue 102 | if len(out[-1]) == N * (K - 2): 103 | out[-1] = "".join(out[-1]) 104 | out.append([]) 105 | out[-1].append(stitched[i][j]) 106 | 107 | out[-1] = "".join(out[-1]) 108 | # forgive my sins 109 | monster = [(0, 18)] + [(1, 0), (1, 5), (1, 6), (1, 11), (1, 12), (1, 17), (1, 18), (1, 19)] + [(2, 1), (2, 4), (2, 7), (2, 10), (2, 13), (2, 16)] 110 | ans = 0 111 | for _ in range(2): 112 | for _ in range(4): 113 | monster_count = 0 114 | for i in range(len(out) - 2): 115 | for j in range(len(out[0]) - 19): 116 | if all(out[i + dx][j + dy] == '#' for dx, dy in monster): 117 | monster_count += 1 118 | ans = max(ans, monster_count) 119 | out = rot(out) 120 | out = flip(out) 121 | 122 | print("part2", "".join(out).count("#") - ans * len(monster)) 123 | 124 | main() 125 | -------------------------------------------------------------------------------- /day21/day21.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int*x,*w,i,j,n,m,y,z,f[2][999],a[999][999],d[999]; 6 | char*p,*q,*r,*s,b[999],g[999][10],l[99][10]; 7 | int main(int c, char**v) { 8 | for(;gets(b)&&*b;){ 9 | for(p=q=b,x=d;*p!='(';++p) 10 | if(*p==' '){ 11 | for(i=0,z=-1;!~z&&i 2 | 3 | typedef struct deck { 4 | int cards[111],len; 5 | } deck_t; 6 | 7 | int pop_deck(deck_t* d){ 8 | int ans = d->cards[0]; 9 | for(int i=0;i+1len;++i){ 10 | d->cards[i]=d->cards[i+1]; 11 | } 12 | --d->len; 13 | return ans; 14 | } 15 | void push(deck_t* d,int v){ 16 | d->cards[d->len++]=v; 17 | } 18 | void copy(deck_t*dst,deck_t*src,int k){ 19 | dst->len=0; 20 | for(int*p=dst->cards,*q=src->cards;k--;++dst->len,++p,++q) 21 | *p=*q; 22 | } 23 | 24 | int game(deck_t* a, deck_t* b,int r){ 25 | deck_t sub1,sub2; 26 | int x,y,z,i; 27 | for(i=0;a->len&&b->len;++i){ 28 | if(i>1000)return 1; 29 | x=pop_deck(a); 30 | y=pop_deck(b); 31 | if(r&&x<=a->len&&y<=b->len){ 32 | copy(&sub1,a,x); 33 | copy(&sub2,b,y); 34 | z=game(&sub1,&sub2,r); 35 | }else{ 36 | z=x>y; 37 | } 38 | z?(push(a,x),push(a,y)):(push(b,y),push(b,x)); 39 | } 40 | return a->len; 41 | } 42 | 43 | int*p; 44 | char b[99]; 45 | deck_t h[2]; 46 | int main(int c,char**v){ 47 | gets(b); 48 | for(p=h[0].cards;scanf(" %d",p++)>0;++h[0].len); 49 | gets(b); 50 | for(p=h[1].cards;scanf(" %d",p++)>0;++h[1].len); 51 | game(&h[0],&h[1],--c); 52 | deck_t* ans = h+(!!h[1].len); 53 | long score = 0; 54 | for(int i=0;ilen;++i){ 55 | score+=(ans->len-i)*ans->cards[i]; 56 | } 57 | printf("%ld\n",score); 58 | } 59 | -------------------------------------------------------------------------------- /day22/day22.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def run_round(a, b, part2): 5 | """Returns true iff player A wins this round""" 6 | x, y = a[0], b[0] 7 | if part2 and x < len(a) and y < len(b): 8 | a, b = run_game(a[1 : x + 1], b[1 : y + 1], part2) 9 | return bool(a) 10 | else: 11 | return x > y 12 | 13 | 14 | def run_game(p0, p1, part2=False): 15 | """Returns the hand states after a player has won""" 16 | seen = set() 17 | a, b = p0, p1 18 | while min(len(p) for p in (a, b)) > 0: 19 | if (a, b) in seen: 20 | return a, b 21 | 22 | seen.add((a, b)) 23 | if run_round(a, b, part2): 24 | a, b = (a[1:] + (a[0], b[0]), b[1:]) 25 | else: 26 | a, b = (a[1:], b[1:] + (b[0], a[0])) 27 | 28 | return a, b 29 | 30 | 31 | def main(): 32 | players = [p.split("\n")[1:] for p in sys.stdin.read().split("\n\n")] 33 | players = tuple(tuple(int(x) for x in p if x) for p in players) 34 | 35 | for part2 in [False, True]: 36 | a, b = run_game(*players, part2=part2) 37 | ans = a if a else b 38 | 39 | n = len(ans) 40 | score = sum((n - i) * x for i, x in enumerate(ans)) 41 | 42 | print(score) 43 | 44 | 45 | main() 46 | -------------------------------------------------------------------------------- /day23/day23.c: -------------------------------------------------------------------------------- 1 | #include/* Pick a cup, any cup. Choose wisely! */ 2 | 3 | // ADVENT OF CODE 2020 DAY 23 || CRAB CUPS // 4 | long n[+1<<20],N=1e6,M,i=9,h,g,x,y,z=49;char s[23];int main( 5 | int c,char**v){for(;i 2 | 3 | using namespace std; 4 | 5 | #ifdef KACTL 6 | #define rep(i, a, b) for(int i = a; i < (b); ++i) 7 | #define trav(a, x) for(auto& a : x) 8 | #define sz(x) (int)(x).size() 9 | #endif 10 | #define all(x) begin(x), end(x) 11 | 12 | using ll = long long; 13 | using ld = long double; 14 | using pii = pair; 15 | using vi = vector; 16 | 17 | constexpr int N = 1e6; 18 | // constexpr int N = 10 - 1; 19 | //constexpr int M = 100; 20 | constexpr int M = 1e7; 21 | struct node { 22 | node* right; 23 | int val; 24 | } nodes[N]; 25 | 26 | bool in_chunk(node*a, node*b, int x) { 27 | for(node*p = a; p != b; p=p->right) { 28 | if(p->val==x) 29 | return true; 30 | } 31 | return false; 32 | } 33 | 34 | int32_t main() { 35 | ios_base::sync_with_stdio(false); 36 | cin.tie(0); cout.tie(0); 37 | 38 | for (int i = 0; i < N; ++i) { 39 | nodes[i].right = &nodes[i + 1]; 40 | nodes[i].val = i + 1; 41 | } 42 | 43 | //string s = "389125467"; 44 | string s = "952438716"; 45 | for (int i = 0; i + 1 < s.size(); ++i) { 46 | nodes[s[i] - '0' - 1].right = &nodes[s[i + 1] - '0' - 1]; 47 | } 48 | 49 | nodes[s.back() - '0' - 1].right = &nodes[s.size()]; 50 | assert(nodes[s.size()].val == 10); 51 | // nodes[s.back() - '0' - 1].right = &nodes[s[0] - '0' - 1]; 52 | nodes[N - 1].right = &nodes[s[0] - '0' - 1]; 53 | assert(nodes[N - 1].val == N); 54 | assert(nodes[N - 1].right->val == s[0]-'0'); 55 | 56 | node * head = &nodes[s[0] - '0' - 1]; 57 | assert(head->val == s[0]-'0'); 58 | for(int iter = 0; iter < M; ++iter) { 59 | /* 60 | bool first=true; 61 | for(node*p=head;first||p!=head;p=p->right){ 62 | cout << p->val << ' '; 63 | first = false; 64 | } cout << '\n'; 65 | */ 66 | 67 | node* rem_start = head->right; 68 | node* oops = head->right->right->right; 69 | node* rem_end = head->right->right->right->right; 70 | 71 | int goal = head->val - 1; 72 | if (goal == 0) 73 | goal = N; 74 | while (in_chunk(rem_start, rem_end, goal)){ 75 | goal = (goal - 1 == 0) ? N : goal - 1; 76 | } 77 | 78 | // cout << "Inserting at goal " << goal << '\n'; 79 | 80 | // printf("After %d comes %d\n", head->val, rem_end->val); 81 | head->right = rem_end; 82 | // printf("After %d comes %d\n", oops->val, nodes[goal - 1].val); 83 | oops->right = nodes[goal - 1].right; 84 | // printf("After %d comes %d\n", nodes[goal-1].val, rem_start->val); 85 | nodes[goal - 1].right = rem_start; 86 | 87 | head = head->right; 88 | } 89 | int i = 0; 90 | for(node*p=&nodes[0];i<10;++i,p=p->right){ 91 | cout << p->val << ' '; 92 | } cout << '\n'; 93 | cout<< '\n'; 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /day23/day23.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, val): 3 | self.val = val 4 | self.next = None 5 | 6 | 7 | def part1(s): 8 | cups = list([int(x) for x in s]) 9 | for _ in range(int(1e2)): 10 | rem = cups[1:4] 11 | goal = cups[0] - 1 if cups[0] > 1 else 9 12 | while goal in rem: 13 | goal -= 1 14 | if goal == 0: 15 | goal = 9 16 | 17 | idx = cups.index(goal) 18 | if idx == 0: 19 | # no change 20 | pass 21 | else: 22 | cups = list([cups[0]] + cups[4 : idx + 1] + rem + cups[idx + 1 :]) 23 | 24 | cups = cups[1:] + [cups[0]] 25 | 26 | print(cups) 27 | 28 | 29 | def part2(s): 30 | N = int(1e6) 31 | nodes = {} 32 | for c in range(1, N + 1): 33 | nodes[c] = Node(c) 34 | 35 | for x, y in zip(s, s[1:]): 36 | x, y = map(int, [x, y]) 37 | nodes[x].next = nodes[y] 38 | 39 | if len(s) == N: 40 | nodes[int(s[-1])].next = nodes[int(s[0])] 41 | else: 42 | nodes[int(s[-1])].next = nodes[len(s) + 1] 43 | for i in range(len(s) + 1, N): 44 | nodes[i].next = nodes[i + 1] 45 | nodes[N].next = nodes[int(s[0])] 46 | 47 | head = nodes[int(s[0])] 48 | for _ in range(int(1e7)): 49 | rem_start = head.next 50 | rem_end = head.next.next.next 51 | 52 | vals = [rem_start.val, rem_start.next.val, rem_end.val] 53 | goal = head.val - 1 if head.val > 1 else N 54 | while goal in vals: 55 | goal -= 1 56 | if goal == 0: 57 | goal = N 58 | 59 | head.next = rem_end.next 60 | rem_end.next = nodes[goal].next 61 | nodes[goal].next = rem_start 62 | 63 | head = head.next 64 | 65 | a, b = nodes[1].next.val, nodes[1].next.next.val 66 | print(a, b, a * b) 67 | 68 | 69 | def main(): 70 | s = "952438716" 71 | s = "389125467" 72 | part1(s) 73 | part2(s) 74 | 75 | 76 | main() 77 | -------------------------------------------------------------------------------- /day24/day24.c: -------------------------------------------------------------------------------- 1 | #include/* count black tiles */ 2 | 3 | // ADVENT OF CODE 2020 || DAY 24: HEX // 4 | int x,y,p,t=100,f,v,w,N=399;char*l,b[99] 5 | ,s[400][400];int main(int c,char**g){for 6 | (;gets(b)&&*b;(s[x][ y]^=1)?++p:--p)for( 7 | x=y=t+t,l=b;*l;++l ){y-=*l=='n';y+=* 8 | l=='s';*l=='e' ||*l=='n'&&*(l 9 | +1)=='e'?++x :*l=='w'||* 10 | l=='s'&&* (l+01)== 11 | 'w'?-- x:0;l 12 | +=*l== 'n'|| 13 | *l==23 *05;} 14 | for(f= --c;f 15 | &&t--; ){for 16 | (x=1;x >=24/1/2/3/4;}printf("%d\n",p);} 24 | -------------------------------------------------------------------------------- /day24/day24.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | dirs = {"e": (1, 0), "w": (-1, 0), "se": (0, 1), "sw": (-1, 1), "ne": (1, -1), "nw": (0, -1)} 10 | 11 | def main(): 12 | state = defaultdict(int) 13 | for line in sys.stdin: 14 | line = line.strip() 15 | i = 0 16 | x, y = 0, 0 17 | while i < len(line): 18 | if line[i] in dirs: 19 | dx, dy = dirs[line[i]] 20 | i += 1 21 | else: 22 | dx, dy = dirs[line[i:i+2]] 23 | i += 2 24 | x, y = x + dx, y + dy 25 | 26 | state[(x, y)] ^= 1 27 | 28 | print("part1", sum(v == 1 for v in state.values())) 29 | for k, v in state.items(): 30 | if v == 1: 31 | print("Seed", k) 32 | 33 | for _ in range(100): 34 | opts = set((x + dx, y + dy) for x, y in state.keys() for dx, dy in dirs.values()) 35 | new_state = defaultdict(int) 36 | for x, y in opts: 37 | old = state[(x, y)] 38 | adj = sum(state[x + dx, y + dy] for dx, dy in dirs.values()) 39 | if old == 1 and adj not in [1, 2]: 40 | new_state[x, y] = 0 41 | elif old == 0 and adj == 2: 42 | new_state[x, y] = 1 43 | else: 44 | new_state[x, y] = old 45 | 46 | state = new_state 47 | print("part2", sum(v == 1 for v in state.values())) 48 | 49 | print(state.keys()) 50 | 51 | main() 52 | -------------------------------------------------------------------------------- /day25/day25.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 4 | long 5 | x,y,M= 6 | 20201227 7 | ,v=1,a,r=1 8 | ;int main(){ 9 | scanf(" %ld%ld" 10 | ,&x,&y);for(;v^x; 11 | ++a)v=7L*v%M;for(;a 12 | ;y=y*y%M,a/=2)a&1?r 13 | =r*y%M 14 | :+5*5; 15 | printf 16 | ("%ld" 17 | "\n",r 18 | );25;} 19 | 20 | // AOC 2020 DAY 25// 21 | -------------------------------------------------------------------------------- /day25/day25.py: -------------------------------------------------------------------------------- 1 | from collections import * 2 | from functools import lru_cache 3 | import heapq 4 | import itertools 5 | import math 6 | import random 7 | import sys 8 | 9 | m = 20201227 10 | 11 | def calc(s, p): 12 | ans = 0 13 | v = 1 14 | while v != p: 15 | ans += 1 16 | v = (s * v) % m 17 | 18 | return ans 19 | 20 | def main(): 21 | p1 = int(input()) 22 | p2 = int(input()) 23 | 24 | ls1 = calc(7, p1) 25 | ls2 = calc(7, p2) 26 | print(ls1, ls2) 27 | print(pow(p2, ls1, m), pow(p1, ls2, m)) 28 | 29 | main() 30 | --------------------------------------------------------------------------------