Skip to content

Instantly share code, notes, and snippets.

@nicholastay
Last active December 23, 2021 13:52
Show Gist options
  • Save nicholastay/fcb589cf29888f038c3e059d24a436d6 to your computer and use it in GitHub Desktop.
Save nicholastay/fcb589cf29888f038c3e059d24a436d6 to your computer and use it in GitHub Desktop.
Advent of Code 2021 for fun in JS
// AoC 2021 for fun in JS (real implementation in rust on my git repo)
// Probably not going to do this past first few days
// -- Day 1 --
// Part 1
input.split('\n').map(Number).map((x, i, arr) => x > arr[i-1]).filter(x => x).length
// Part 2
input.split('\n').map(Number).map((x, i, arr) => arr[i+1] + arr[i-1] > arr[i-1] + arr[i-2]).filter(x => x).length
// -- Day 2 --
// Part 1
input.split('\n').map(x => x.split(" ")).map(([d,v])=>[d[0],Number(v)]).reduce(([oh,od],[d,v]) => d == 'f' ? [oh+v,od] : d == 'd' ? [oh,od+v] : [oh,od-v], [0, 0]).reduce((x,y)=>x*y)
// Part 2
input.split('\n').map(x => x.split(" ")).map(([d,v])=>[d[0],Number(v)]).reduce(([oh,od,a],[d,v]) => d == 'f' ? [oh+v,od+a*v,a] : d == 'd' ? [oh,od,a+v] : [oh,od,a-v], [0, 0, 0]).slice(0, 2).reduce((x,y)=>x*y)
// -- Day 3 --
// Part 1
input.split('\n').map(x => x.split("").map(Number)).map((_, i, arr) => arr.map(row => row[i])).filter(r=>!r.includes(undefined)).map(r => r.filter(x=>x==1).length > r.filter(x=>x==0).length ? 1 : 0).reduce(([g,e], x) => [(g<<1)+x,(e<<1)+(x==1?0:1)], [0, 0]).reduce((x,y)=>x*y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment