Skip to content

Instantly share code, notes, and snippets.

@carlbennettnz
Last active December 10, 2022 23:42
Show Gist options
  • Save carlbennettnz/1bf2ea571811b688347e218d09465864 to your computer and use it in GitHub Desktop.
Save carlbennettnz/1bf2ea571811b688347e218d09465864 to your computer and use it in GitHub Desktop.
document.body.textContent.split('\n').reduce(([best, curr], line) => line ? [best, curr + 1 * line] : [Math.max(best, curr), 0], [0,0])[0]
document.body.textContent.split('\n').reduce(([totals, curr], line) => line ? [totals, curr + 1 * line] : [[...totals, curr], 0], [[],0])[0].sort((a, b) => b - a).slice(0, 3).reduce((x, y) => x + y)
document.body.textContent.trim().split('\n').map(line => line.split(' ').map(choice => ['A', 'B', 'C', 'X', 'Y', 'Z'].indexOf(choice) % 3)).map(([them, me]) => me + 1 + [3, 6, 0][(3 + me - them) % 3]).reduce((a, b) => a + b)
document.body.textContent.trim().split('\n').map(line => line.split(' ').map(choice => ['A', 'B', 'C', 'X', 'Y', 'Z'].indexOf(choice) % 3)).map(([them, result]) => [(2 + them + result) % 3, result]).map(([me, result]) => me + 1 + [0, 3, 6][result]).reduce((a, b) => a + b)
document.body.textContent.trim().split('\n').flatMap(line => line.split('').filter((item, i) => i < line.length / 2 && !line.slice(0, i).includes(item) && line.slice(line.length / 2).includes(item)).map(item => item.charCodeAt(0)).map(item => item > 96 ? item - 96 : item - 64 + 26)).reduce((a, b) => a + b)
document.body.textContent.trim().split('\n').flatMap((line, i, arr) => i % 3 === 2 ? [line.split('').find(item => arr[i - 1].includes(item) && arr[i - 2].includes(item))] : []).map(item => item.charCodeAt(0)).map(item => item > 96 ? item - 96 : item - 64 + 26).reduce((a, b) => a + b)
document.body.textContent.trim().split('\n').map(line => line.match(/\d+/g).map(Number)).filter(([a1, a2, b1, b2]) => (a1 <= b1 && a2 >= b2) || (a1 >= b1 && a2 <= b2)).length
document.body.textContent.trim().split('\n').map(line => line.match(/\d+/g).map(Number)).filter(([a1, a2, b1, b2]) => a1 <= b2 && a2 >= b1).length
(([stateStr, instructionsStr]) => instructionsStr.map(line => line.match(/\d+/g).map(Number)).map(([r, f, t]) => [r, f-1, t-1]).reduce((state, [r, f, t]) => state.map((col, i) => f === i ? col.slice(0, -r) : (t === i ? [...state[i], ...state[f].slice(-r).reverse()] : state[i])), [...Array(stateStr.at(-1).match(/\d+/g).map(Number).at(-1))].map((_, i) => stateStr.slice(0, -1).map(line => line[i*4+1]).filter(item => item && item !== ' ').reverse())).map(col => col.at(-1)).join(''))(document.body.textContent.split('\n\n').map(s => s.trimEnd().split('\n')))
(([stateStr, instructionsStr]) => instructionsStr.map(line => line.match(/\d+/g).map(Number)).map(([r, f, t]) => [r, f-1, t-1]).reduce((state, [r, f, t]) => state.map((col, i) => f === i ? col.slice(0, -r) : (t === i ? [...state[i], ...state[f].slice(-r)] : state[i])), [...Array(stateStr.at(-1).match(/\d+/g).map(Number).at(-1))].map((_, i) => stateStr.slice(0, -1).map(line => line[i*4+1]).filter(item => item && item !== ' ').reverse())).map(col => col.at(-1)).join(''))(document.body.textContent.split('\n\n').map(s => s.trimEnd().split('\n')))
document.body.textContent.split('').findIndex((c, i, a) => new Set(a.slice(i, i + 4)).size === 4) + 4
document.body.textContent.split('').findIndex((c, i, a) => new Set(a.slice(i, i + 14)).size === 14) + 14
((files, dirs) => [...dirs].map(dir => [dir, Object.entries(files).map(([file, size]) => file.startsWith(dir) ? size : 0).reduce((a, b) => a + b, 0)]).filter(([dir, size]) => size <= 100000).map(([dir, size]) => size).reduce((a, b) => a + b, 0))(...document.body.textContent.trim().split('\n').slice(1).flatMap(line => line.startsWith('$ cd') ? [{ type: 'cd', value: line.slice(5) }] : line.startsWith('$ ls') ? [] : line.startsWith('dir ') ? [{ type: 'dir', value: line.split(' ')[1]}] : [{ type: 'file', value: {size: Number(line.split(' ')[0]), name: line.split(' ')[1]} }]).reduce(([loc, files, dirs], { type, value }) => type === 'cd' ? [value === '..' ? loc.slice(0, -1) : [...loc, value], files, dirs] : type === 'dir' ? [loc, files, dirs.add(loc.map(l => `/${l}`).join('') + '/')] : [ loc, { ...files, [`${loc.map(l => `/${l}`).join('')}/${value.name}`]: value.size }, loc.map((_, i, p) => p.slice(0, i+1).map(x => `/${x}`).join('') + '/').reduce((ds, d) => ds.add(d), dirs) ], [[], {}, new Set()]).slice(1))
((files, dirs) => [...dirs].map(dir => [dir, Object.entries(files).map(([file, size]) => file.startsWith(dir) ? size : 0).reduce((a, b) => a + b, 0)]).filter(([dir, size]) => size >= 30000000 - (70000000 - 49192532)).sort((a, b) => a[1] - b[1])[0][0])(...document.body.textContent.trim().split('\n').slice(1) .flatMap(line => line.startsWith('$ cd') ? [{ type: 'cd', value: line.slice(5) }] : line.startsWith('$ ls') ? [] : line.startsWith('dir ') ? [{ type: 'dir', value: line.split(' ')[1]}] : [{ type: 'file', value: {size: Number(line.split(' ')[0]), name: line.split(' ')[1]} }]).reduce(([loc, files, dirs], { type, value }) => type === 'cd' ? [value === '..' ? loc.slice(0, -1) : [...loc, value], files, dirs] : type === 'dir' ? [loc, files, dirs.add(loc.map(l => `/${l}`).join('') + '/')] : [ loc, { ...files, [`${loc.map(l => `/${l}`).join('')}/${value.name}`]: value.size }, loc.map((_, i, p) => p.slice(0, i+1).map(x => `/${x}`).join('') + '/').reduce((ds, d) => ds.add(d), dirs) ], [[], {}, new Set()]).slice(1))
document.body.textContent.trim().split('\n').flatMap(line => line === "noop" ? [line] : ['noop', line]).reduce(([total, register], line, cycle) => [(cycle + 21) % 40 ? total : total + (cycle + 1) * register, line.startsWith('addx') ? register + 1*line.split(' ')[1] : register], [0, 1])[0]
document.body.textContent.trim().split('\n').flatMap(line => line === "noop" ? [line] : ['noop', line]).reduce(([crt, register], line, cycle) => [Object.values({...crt, [cycle]: Math.abs(register - cycle % 40) <= 1 ? '#' : '.'}), line.startsWith('addx') ? register + 1*line.split(' ')[1] : register], [Array(240).fill('.'), 1])[0].reduce((output, pixel, i) => i % 40 ? output + pixel : output + '\n' + pixel, '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment