Last active
December 7, 2021 08:49
-
-
Save koopa/4b0e97ec4fde889dfb9ea9433675e180 to your computer and use it in GitHub Desktop.
advent 2021
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1 - I | |
document.body.textContent.split("\n").map(Number).reduce( | |
(acc, it) => it > acc[0] | |
? [it, acc[1]+1] $ | |
: [it, acc[1]], | |
[9999, 0])[1] | |
//1 - II | |
(nums = document.body.textContent.split("\n").map(Number)).splice(3).reduce( | |
(acc, it) => | |
[ | |
[...acc[0].slice(1), it], | |
[...acc[1],acc[0].reduce((a,i)=>a+i)] | |
], | |
[nums, []])[1].reduce( | |
(acc, it) => it > acc[0] | |
? [it, acc[1]+1] | |
: [it, acc[1]], | |
[9999, 0])[1] | |
//2 - I | |
document.body.textContent.split("\n").reduce((acc, it) => { | |
if(!it) return acc | |
[action, steps] = it.split(" ") | |
steps = parseInt(steps); | |
switch(action){ | |
case 'forward': acc[0]+=steps; break; | |
case 'up': acc[1]-=steps;break; | |
case 'down': acc[1]+=steps;break; | |
} | |
return acc; | |
}, [0,0]) | |
//2 - II | |
document.body.textContent.split("\n").reduce((acc, it) => { | |
if(!it) return acc; | |
[action, steps] = it.split(" ") | |
steps = parseInt(steps); | |
if(action == "forward" && acc[2] == 0){ | |
acc[0] += steps; | |
return acc; | |
} | |
switch(action){ | |
case 'forward': | |
acc[0] += steps; | |
d = steps * acc[2] | |
acc[1] = acc[1] + (acc[2] > 0 ? d : -d); | |
break; | |
case 'up': acc[2] -= steps;break; | |
case 'down': acc[2] += steps;break; | |
} | |
return acc; | |
}, [0,0,0]) | |
//3 - I | |
rows = document.body.textContent.split("\n"), | |
w = rows[0].length, h = rows.length, gamma = [] | |
for(let i = 0; i < w; i++){ | |
let zeroes = 0 | |
for(let j = 0; j < h; j++) rows[j][i] == '0' && zeroes++ | |
gamma[i] = zeroes > h / 2 ? '0' : '1' | |
} | |
gamma = parseInt(gamma.join(""), 2) | |
epsilon = 2**w - 1 - gamma | |
epsilon*gamma | |
//3 - II | |
rows=document.body.textContent.split("\n") | |
w = rows[0].length | |
, h = rows.length; | |
calc = (arr, w, order = ['0', '1']) => { | |
for(let i = 0; i < w; i++){ | |
zeroes = 0; | |
for(let j = 0; j < arr.length; j++) if(arr[j][i] == '0') zeroes++ | |
for(let j = 0; j < arr.length; j++) | |
if(zeroes > arr.length / 2 && arr[j][i] == order[0] | |
|| zeroes <= arr.length / 2 && arr[j][i] == order[1]) delete arr[j] | |
arr=arr.filter(a=>a) | |
if(arr.length == 1) break; | |
} | |
return arr | |
} | |
parseInt(calc([...rows], w),2)*parseInt(calc([...rows], w, ['1', '0']), 2) | |
// 4 - I | |
size=5,input=document.body.textContent.split(/\s+/g) | |
nums=input.shift().split(',') | |
boards=input.map(e=>[e,0]).reduce((acc, item, i) => { | |
!acc[(c=~~(i/(size**2)))] && (acc[c] = [item]) || acc[c].push(item) | |
return acc | |
}, []) | |
done = 0; | |
nums.some(n=>{ | |
boards.some((b,bi) => { | |
if((f = b.map(i=>i[0]).indexOf(n)) > -1 && boards[bi][f]) boards[bi][f][1] = 1 | |
for(i=0;i<size;i++) if(b.filter((_,x) => x % size == i).filter(it=>it[1]).length == size | |
|| b.filter((_,x) => x % size == ~~(i/5) % 5).filter(it=>it[1]).length == size | |
|| b.slice(i*size, (i+1)*size).filter(it=>it[1]).length == size) | |
done = [bi, n] | |
}) | |
return done | |
}) | |
boards[done[0]].map(a=>[+a[0],a[1]]).reduce((acc,i)=>i[1]?acc:acc+i[0], 0) * done[1] | |
// 4 - II | |
size=5,input=document.body.textContent.split(/\s+/g).filter(a=>a) | |
nums=input.shift().split(',') | |
boards=input.map(e=>[e,0]).reduce((acc, item, i) => { | |
!acc[(c=~~(i/(size**2)))] && (acc[c] = [item]) || acc[c].push(item) | |
return acc | |
}, []) | |
done = 0 | |
nums.some(n=>{ | |
boards.some((b,bi) => { | |
if((f = b.map(i=>i[0]).indexOf(n)) > -1 && boards[bi][f]) boards[bi][f][1] = 1 | |
for(i=0;i<size;i++) if(b.filter((_,x) => x % size == i).filter(it=>it[1]).length == size | |
|| b.filter((_,x) => x % size == ~~(i/5) % 5).filter(it=>it[1]).length == size | |
|| b.slice(i*size, (i+1)*size).filter(it=>it[1]).length == size) | |
boards.filter(a=>a).length > 1 ? delete boards[bi] : (done = boards.filter(a=>a).length == 1 ? n : 0) | |
}) | |
return done | |
}) | |
boards.filter(a=>a)[0].map(a=>[+a[0],a[1]]).reduce((acc,i)=>i[1]?acc:acc+i[0], 0)*done | |
//5 - I/II | |
document.body.textContent.split("\n").map(it=>it.split(/ -> |,/).map(Number)).reduce((acc, it) => { | |
if(it[0] != it[2] && it[1] != it[3]) return acc //comment this line out for I | |
ax=it[0],bx=it[2],ay=it[1],by=it[3],dx=bx-ax,dy=by-ay | |
s=(a=Math.abs)(dx)>a(dy)?a(dx):a(dy),sx=dx/s,sy=dy/s | |
for(i=0;i<s;i++) acc[idx=ax+(ay*1e6)]=acc[idx]+1||1 | |
ax+=sx,ay+=sy acc[idx=ax+(ay*1e6)]=acc[idx]+1||1 | |
return acc | |
}, []).filter(i=>i>1).length | |
//6 - I/II | |
f=document.body.textContent.split(',').map(Number).reduce((a,i)=>(++a[i],a), new Array(9).fill(0)),d=256 //d=80 for I | |
while(d--)(n=f.shift(),f[8]=n,f[6]+=n) | |
f.reduce((a,i)=>a+i) | |
//7 - I | |
n=document.body.textContent.split(",").map(Number) | |
r=Infinity | |
for(t=0;t<n.length;t++) r=Math.min(r, n.reduce((a,i)=>a+Math.abs(i-t), 0)) | |
//7 - II | |
n=document.body.textContent.split(",").map(Number) | |
r=Infinity,f=n=>n<=1?1:n+f(n-1) | |
for(t=0;t<n.length;t++) r=Math.min(r, n.reduce((a,i)=>a+f(Math.abs(i-t)), 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment