You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letinput=awaitfetch('https://adventofcode.com/2019/day/1/input').then(r=>r.text());input=input.split('\n').filter(Boolean);// Part 1input.reduce((acc,curr)=>{constres=(parseInt(curr)/3|0)-2;returnacc+BigInt(res);},0n);// Part 2functionfuel(n){n=parseInt(n);n=(n/3|0)-2;returnn<=0 ? 0 : n+fuel(n);}input.reduce((acc,curr)=>{returnacc+BigInt(fuel(curr));},0n);
4: Secure Container
constinput='234208-765869';const[from,to]=input.split('-').map(v=>parseInt(v));// Part 1constreDuplicate=/([0-9])\1{1}/;functiontest(n){constnStr=n.toString();if(!reDuplicate.test(nStr))returnfalse;// Going from left to right, the digits never decreasereturnnStr.split('').map(x=>x|0).reduce((acc,curr,i,digits)=>digits[i-1]
? (curr>=digits[i-1])&&acc
: acc,true);}letcount=0;for(leti=from;i<to;i++){count+=test(i);}// Part 2functiontest(n){constnStr=n.toString();// Two adjacent matching digits are not part of a larger group of matching digitsconstdoubles=nStr.match(/([0-9])\1+/g)?.filter(m=>m.length===2);if(!doubles||!doubles.length)returnfalse;// Going from left to right, the digits never decreasereturnnStr.split('').map(x=>x|0).reduce((acc,curr,i,digits)=>digits[i-1]
? (curr>=digits[i-1])&&acc
: acc,true);}letcount=0;for(leti=from;i<to;i++){count+=test(i);}