Skip to content

Instantly share code, notes, and snippets.

@willowiscool
Created December 23, 2023 08:52
Show Gist options
  • Save willowiscool/69894544c068fcd656fe8a760f08de3d to your computer and use it in GitHub Desktop.
Save willowiscool/69894544c068fcd656fe8a760f08de3d to your computer and use it in GitHub Desktop.
advent of code 2023 in javascript one-liners
// day 1 part 1
document.body.innerText.trim().split("\n").map(e=>Number(/^\D*(\d)/.exec(e)[1]+/(\d)\D*$/.exec(e)[1])).reduce((a,b)=>a+b)
// day 1 part 2
document.body.innerText.trim().split("\n").map(e=>e.replace(/one|two|three|four|five|six|seven|eight|nine/g,d=>[0,"one","two","three","four","five","six","seven","eight","nine"].indexOf(d))).map(e=>Number(/^\D*(\d)/.exec(e)[1]+/(\d)\D*$/.exec(e)[1])).reduce((a,b)=>a+b)
// you could golf this more --- something like ...replace(new RegExp((a=[...]).join`|`,"g"),d=>a.indexOf(d))...
// day 2 part 1
document.body.innerText.trim().split("\n").filter(g=>g.split(";").map(d=>(Number((/(\d+) b/.exec(d)||[0,0])[1])<=14)&&(Number((/(\d+) g/.exec(d)||[0,0])[1])<=13)&&(Number((/(\d+) r/.exec(d)||[0,0])[1])<=12)).reduce((a,b)=>a&&b)).map(g=>Number(g.match(/\d+/)[0])).reduce((a,b)=>a+b)
// day 2 part 2
document.body.innerText.trim().split("\n").map(g=>g.split(";").map(d=>[Number((/(\d+) r/.exec(d)||[0,0])[1]),Number((/(\d+) g/.exec(d)||[0,0])[1]),Number((/(\d+) b/.exec(d)||[0,0])[1])]).reduce((a,b)=>[Math.max(a[0],b[0]),Math.max(a[1],b[1]),Math.max(a[2],b[2])]).reduce((a,b)=>a*b)).reduce((a,b)=>a+b)
// probably could be golfed a LOT more, esp. reducing redundant regex (maybe quick function to generate regexes given a letter r, g, b and then map ["r","g","b"] on it? or that might be too much) and reducing redundant Math.max
// day 3 part 1
i = document.body.innerText.trim()
i.split("\n").map((e,l)=>[...e.matchAll(/\d+/g)].filter(n=>/[^\d.]/g.exec(i.split("\n")[Math.max(l-1,0)].substring(Math.max(n.index-1,0),Math.min(n.index+n[0].length+1,e.length-1))+i.split("\n")[Math.min(l+1,e.length-1)].substring(Math.max(n.index-1,0),Math.min(n.index+n[0].length+1,e.length-1))+(n.index!==0?e[n.index-1]:"")+((n.index+n[0].length)!==e.length?e[n.index+n[0].length]:""))).map(n=>Number(n[0])).reduce((a,b)=>a+b,0)).reduce((a,b)=>a+b)
// this is a MESS!! you could definitely replace some of the Math.max/Math.mins. Also, here is where we start stretching the definition of a one-liner. I think it still counts---all you need to do is put the input string into a variable.
// figured this one was a lot less self explanatory so here goes. Basically, the problem is reduced to "find all of the numbers, and filter out the numbers that aren't adjacent to a symbol," that second part turing into "filter out the numbers for which there is no symbol in the surrounding character string". So exec gets passed this string, which only ends up looking so gargantuan because of edge cases (literally---numbers at the edge of the array). This is also why the input needs to be stored in a variable---it needs to look back into the input to see the surrounding characters.
// NOTE: this solution assumes all of the lines in the input are of the same length! (which is the case for the advent of code input)
// day 3 part 2
[...i.matchAll(/\*/g)].map(g=>[Math.floor(g.index/(i.indexOf("\n")+1)),g.index%(i.indexOf("\n")+1),g.index]).map(g=>[...i.split("\n")[g[0]].matchAll(/(\d*)\*(\d*)/g)].filter(e=>e.index<=g[1]&&(e.index+e[0].length)>g[1])[0].slice(1).concat([...i.split("\n")[Math.max(g[0]-1,0)].matchAll(/\d+/g),...i.split("\n")[Math.min(g[0]+1,i.split("\n").length-1)].matchAll(/\d+/g)].filter(e=>e.index<=g[1]+1&&(e.index+e[0].length)>=g[1]).map(e=>e[0])).filter(e=>e!=="")).filter(e=>e.length===2).map(e=>Number(e[0])*Number(e[1])).reduce((a,b)=>a+b)
// this one is also atrocious but probably less so??? because I did split it up... at least a little bit...
// so basically the first step is to find all of the asterisks (gears), and what the first map does is turn them into [line, position within that line, position within the input string]
// then, step two is to find all of the surrounding numbers
// 2.1: finding numbers before and after the * on the same line - covered by the part from the 2nd map to the concat - uses regex to match all <number>?*<number>? strings on the line the * is on, filters for only the one that the * in question covers, and extracts just the numbers adjacent to it (or empty strings)
// 2.2: finding numbers above and below - covered by the part after the concat within the second map - somehow easier! just match numbers on the lines before and after the *, and then filter for only the ones whose positions are around the *'s position.
// step three - filter out empty strings, filter out not-gears (have to have exactly 2 adjacent numbers), turn into gear ratios, sum
// making this more efficient - very possible, and probably not even that hard! there is a redundant use of filter!
// day 4 part 1
document.body.innerText.trim().split("\n").map(l=>l.split(/:|\|/g).map(a=>a.trim().split(/\s+/g))).map(c=>Math.floor(2**(c[2].filter(n=>c[1].includes(n)).length-1))).reduce((a,b)=>a+b)
// day 4 part 2
i = document.body.innerText.trim()
i.split("\n").map(l=>l.split(/:|\|/g).map(a=>a.trim().split(/\s+/g))).reduce((a,c)=>[a[0]+a[1][0],a[1].slice(1).map((o,i)=>o+(c[2].filter(n=>c[1].includes(n)).length>i?1:0)*a[1][0])],[0,Array(i.split("\n").length).fill(1)])[0]
// I am actually so proud of this one like I was gonna go to sleep but the solution popped into my head and I couldn't rest until I could write it down
// basically, it uses the reduce method on the array of cards from earlier, but with an accumulator that is [# of total scratchcards, [# of copies of next card, # of copies of 2nd card down, # of copies of 3rd card down...]]
// thus, reduce is initialized with 0 total scratchcards and 1 copy of each scratchcard in the remaining array, and that is slowly reduced down (adding current scratchcards to total, and then adding won copies to the existing # of copies array) until there are no more scratchcards left!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment