Last active
January 1, 2021 00:10
-
-
Save meooow25/e10083d0e7fa56959e8f265c80e9ae91 to your computer and use it in GitHub Desktop.
Advent of Code 2020
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
document.body.textContent.trim().split('\n').map(x => Number(x)) | |
.flatMap((x, i, a) => a.flatMap((y, j) => i < j && x + y === 2020 ? [x * y] : [])) |
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
document.body.textContent.trim().split('\n').map(x => Number(x)) | |
.flatMap((x, i, a) => | |
a.flatMap((y, j) => | |
a.flatMap((z, k) => i < j && j < k && x + y + z === 2020 ? [x * y * z]: []))) |
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
document.body.textContent.trim().split('\n') | |
.filter(x => { | |
[_, l, h, c, s] = x.match(/(\d+)-(\d+) (\w): (\w+)/); | |
y = s.split(c).length - 1; | |
return Number(l) <= y && y <= Number(h); | |
}).length |
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
document.body.textContent.trim().split('\n') | |
.filter(x => { | |
[_, l, h, c, s] = x.match(/(\d+)-(\d+) (\w): (\w+)/); | |
return s[l - 1] === c ^ s[h - 1] === c; | |
}).length |
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
document.body.textContent.trim().split('\n').reduce((acc, val, idx) => acc + (val[(3 * idx) % val.length] == '#'), 0) |
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, 1], [3, 1], [5, 1], [7, 1], [1, 2]] | |
.map(([right, down]) => document.body.textContent.trim().split('\n') | |
.reduce((acc, val, idx) => acc + (idx % down ? 0 : val[(right * idx / down) % val.length] == '#'), 0)) | |
.reduce((acc, val) => acc * val) |
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
document.body.textContent.trim().split('\n\n') | |
.map(x => new Map(x.split(/\s/).map(y => y.split(':')))) | |
.map(x => 'byr iyr eyr hgt hcl ecl pid'.split(' ').every(f => x.has(f))) | |
.reduce((acc, val) => acc + val) |
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
fields = { | |
byr: x => /^[0-9]{4}$/.test(x) && 1920 <= Number(x) && Number(x) <= 2002, | |
iyr: x => /^[0-9]{4}$/.test(x) && 2010 <= Number(x) && Number(x) <= 2020, | |
eyr: x => /^[0-9]{4}$/.test(x) && 2020 <= Number(x) && Number(x) <= 2030, | |
hgt: x => { | |
y = Number(x.slice(0, -2)); | |
return x.slice(-2) === 'cm' ? 150 <= y && y <= 193 : x.slice(-2) === 'in' && 59 <= y && y <= 76; | |
}, | |
hcl: x => /^#[0-9a-f]{6}$/.test(x), | |
ecl: x => 'amb blu brn gry grn hzl oth'.split(' ').includes(x), | |
pid: x => /^[0-9]{9}$/.test(x), | |
}; | |
document.body.textContent.trim().split('\n\n') | |
.map(x => new Map(x.split(/\s/).map(y => y.split(':')))) | |
.map(x => Object.entries(fields).every(([f, g]) => x.has(f) && g(x.get(f)))) | |
.reduce((acc, val) => acc + val) |
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
document.body.textContent.trim().split('\n') | |
.map(x => parseInt(x.replaceAll(/[FBLR]/g, c => { return {F: 0, B: 1, L: 0, R: 1}[c]; }), 2)) | |
.reduce((acc, val) => Math.max(acc, val)) |
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
document.body.textContent.trim().split('\n') | |
.map(x => parseInt(x.replaceAll(/[FBLR]/g, c => { return {F: 0, B: 1, L: 0, R: 1}[c]; }), 2)) | |
.sort((a, b) => a - b) | |
.filter((x, i, a) => a[i + 1] === x + 2)[0] + 1 |
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
document.body.textContent.trim().split('\n\n') | |
.map(x => new Set(x.split('\n').join('')).size) | |
.reduce((acc, val) => acc + val) |
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
document.body.textContent.trim().split('\n\n') | |
.map(x => x.split('\n') | |
.map(y => Array.from(y)) | |
.reduce((acc, val) => acc.filter(y => val.includes(y))).length) | |
.reduce((acc, val) => acc + val) |
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
g = document.body.textContent.trim().split('\n') | |
.map(x => x.replaceAll(/ bags?\.?/g, '').split(' contain ')) | |
.flatMap(([u, v]) => v.split(', ').map(y => [u, y.replace(/\d /g, '')])) | |
.reduce((g, [u, v]) => g.has(v) ? g.get(v).push(u) && g : g.set(v, [u]), new Map()); | |
dfs = u => (g.get(u) ?? []).reduce((acc, val) => dfs(val).forEach(x => acc.add(x)) ?? acc, new Set([u])) | |
dfs('shiny gold').size - 1 |
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
g = new Map(document.body.textContent.trim().split('\n') | |
.map(x => x.replaceAll(/ bags?\.?/g, '').split(' contain ')) | |
.map(([u, v]) => [u, v.split(', ').filter(y => y !== 'no other') | |
.map(y => [Number(y.match(/\d/)[0]), y.replace(/\d /, '')])])); | |
dfs = u => 1 + g.get(u).reduce((acc, [c, v]) => acc + c * dfs(v), 0); | |
dfs('shiny gold') - 1 |
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
z = document.body.textContent.trim().split('\n').map(x => x.split(' ')).map(x => [x[0], Number(x[1])]); | |
nxt = (i, a, x, y) => x === 'nop' ? [i + 1, a] : x === 'acc' ? [i + 1, a + y] : [i + y, a]; | |
f = (i, a, s) => s.has(i) ? a : s.add(i) && f(...nxt(i, a, ...z[i]), s); | |
f(0, 0, new Set()) |
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
z = document.body.textContent.trim().split('\n').map(x => x.split(' ')).map(x => [x[0], Number(x[1])]); | |
nxt = (i, a, x, y) => x === 'nop' ? [i + 1, a] : x === 'acc' ? [i + 1, a + y] : [i + y, a]; | |
f = (i, a, s) => i == z.length ? [a] : s.has(i) ? [] : s.add(i) && f(...nxt(i, a, ...z[i]), s); | |
swap = i => z[i][0] = z[i][0].replace(/(nop|jmp)/, x => x === 'nop' ? 'jmp' : 'nop'); | |
z.flatMap((_, i) => (swap(i), r = f(0, 0, new Set()), swap(i), r)) |
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
a = document.body.textContent.trim().split('\n').map(x => Number(x)) | |
a.slice(0, -25) | |
.map((_, i) => a.slice(i, i + 25)) | |
.map(x => x.flatMap((y, i) => x.flatMap((z, j) => i < j ? [y + z] : []))) | |
.flatMap((x, i) => x.includes(a[i + 25]) ? [] : [a[i + 25]]) |
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
a = document.body.textContent.trim().split('\n').map(x => Number(x)) | |
t = a.slice(0, -25) | |
.map((_, i) => a.slice(i, i + 25)) | |
.map(x => x.flatMap((y, i) => x.flatMap((z, j) => i < j ? [y + z] : []))) | |
.flatMap((x, i) => x.includes(a[i + 25]) ? [] : [a[i + 25]])[0] | |
a.flatMap((_, i) => a.flatMap((_, j) => i < j ? [a.slice(i, j + 1)] : [])) | |
.flatMap(x => x.reduce((acc, val) => acc + val) === t ? [Math.min(...x) + Math.max(...x)] : []) |
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
c = document.body.textContent.trim().split('\n').map(x => Number(x)) | |
.sort((a, b) => a - b) | |
.map((x, i, a) => a[i + 1] - x).slice(0, -1) | |
.reduce((acc, val) => (acc[val] += 1, acc), [0, 0, 0, 1]) | |
c[1] * c[3] |
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
document.body.textContent.trim().split('\n').map(x => Number(x)) | |
.sort((a, b) => a - b) | |
.reduce((acc, x, i, a) => | |
(acc[i] = x < 4, [i - 1, i - 2, i - 3].forEach(j => acc[i] += x - a[j] < 4 ? acc[j] : 0), acc), []) | |
.slice(-1) |
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
a = document.body.textContent.trim().split('\n').map(x => Array.from(x)); | |
fl = a.map(x => x.map(y => y === '.')); | |
h = a.map(x => x.map(y => y === '#')); | |
dirs = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]; | |
occ = (i, j) => dirs.reduce((acc, [x, y]) => acc + (h[i + x] && h[i + x][j + y] || 0), 0); | |
while (1) { | |
h2 = h.reduce((acc, x, i) => | |
(acc[i] = x.reduce((acc2, y, j) => | |
(acc2[j] = fl[i][j] ? 0 : y ? occ(i, j) >= 4 ? 0 : 1 : occ(i, j) === 0 ? 1 : 0, acc2), []), acc), []); | |
if (JSON.stringify(h) === JSON.stringify(h2)) { | |
break; | |
} | |
h = h2; | |
} | |
JSON.stringify(h).split(1).length - 1 |
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
a = document.body.textContent.trim().split('\n').map(x => Array.from(x)); | |
fl = a.map(x => x.map(y => y === '.')); | |
h = a.map(x => x.map(y => y === '#')); | |
dirs = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]; | |
occ = (i, j) => dirs.reduce((acc, [x, y]) => { | |
cx = i, cy = j; | |
while (1) { | |
cx += x, cy += y; | |
if (h[cx] == null || h[cx][cy] == null) return acc; | |
if (!fl[cx][cy]) return acc + h[cx][cy]; | |
} | |
}, 0); | |
while (1) { | |
h2 = h.reduce((acc, x, i) => | |
(acc[i] = x.reduce((acc2, y, j) => | |
(acc2[j] = fl[i][j] ? 0 : y ? occ(i, j) >= 5 ? 0 : 1 : occ(i, j) === 0 ? 1 : 0, acc2), []), acc), []); | |
if (JSON.stringify(h) === JSON.stringify(h2)) { | |
break; | |
} | |
h = h2; | |
} | |
JSON.stringify(h).split(1).length - 1 |
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
document.body.textContent.trim().split('\n') | |
.map(x => x.match(/(.)(.*)/)) | |
.map(x => [x[1], Number(x[2])]) | |
.reduce(([x, y, dirx, diry], [p, q]) => { | |
x += q * (({E: 1, W: -1}[p] ?? 0) + (p === 'F') * dirx); | |
y += q * (({N: 1, S: -1}[p] ?? 0) + (p === 'F') * diry); | |
m = {L: 1, R: -1}[p] ?? 0; | |
for (i = 0; m && i < q / 90; i++) [dirx, diry] = [-m * diry, m * dirx]; | |
return [x, y, dirx, diry]; | |
}, [0, 0, 1, 0]) | |
.slice(0, 2) | |
.reduce((acc, val) => acc + Math.abs(val), 0); |
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
document.body.textContent.trim().split('\n') | |
.map(x => x.match(/(.)(.*)/)) | |
.map(x => [x[1], Number(x[2])]) | |
.reduce(([x, y, dirx, diry], [p, q]) => { | |
dirx += q * ({E: 1, W: -1}[p] ?? 0); | |
diry += q * ({N: 1, S: -1}[p] ?? 0); | |
if (p === 'F') x += q * dirx, y += q * diry; | |
m = {L: 1, R: -1}[p] ?? 0; | |
for (i = 0; m && i < q / 90; i++) [dirx, diry] = [-m * diry, m * dirx]; | |
return [x, y, dirx, diry]; | |
}, [0, 0, 10, 1]) | |
.slice(0, 2) | |
.reduce((acc, val) => acc + Math.abs(val), 0); |
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
[n, a] = document.body.textContent.trim().split('\n'); | |
[n, a] = [Number(n), a.split(',').filter(x => x !== 'x').map(x => Number(x))]; | |
a.map(x => [x * Math.ceil(n / x) - n, x]) | |
.reduce((acc, [y, x]) => y < acc[0] ? [y, x * y] : acc, [Infinity]) |
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
egcd = (a, b) => { | |
if (!b) return [1n, 0n, a]; | |
[x, y, g] = egcd(b, a % b); | |
return [y, x - y * (a / b), g]; | |
}; | |
document.body.textContent.trim().split('\n')[1] | |
.split(',') | |
.flatMap((x, i) => x === 'x' ? [] : [[BigInt(x), BigInt(-i)]]) | |
.reduce(([y, offy], [x, offx]) => { | |
[px, py, g] = egcd(x, y); // all primes, g = 1 | |
lcm = x * y; | |
pos = ((offy - offx) * px * x + offx) % lcm; | |
return [lcm, (pos + lcm) % lcm]; | |
}) |
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
Array.from( | |
document.body.textContent.trim().split('\n') | |
.reduce(([mem, ma, mo], inp) => { | |
[_, m] = inp.match(/mask = ([01X]+)/) ?? []; | |
if (m) { | |
ma = BigInt('0b' + m.replaceAll('X', 1)), mo = BigInt('0b' + m.replaceAll('X', 0)); | |
} else { | |
[_, pos, val] = inp.match(/mem\[(\d+)\] = (\d+)/); | |
pos = BigInt(pos), val = BigInt(val); | |
mem.set(pos, ma & val | mo); | |
} | |
return [mem, ma, mo]; | |
}, [new Map()])[0].values()) | |
.reduce((acc, val) => acc + val) |
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
Array.from( | |
document.body.textContent.trim().split('\n') | |
.reduce(([mem, ma, mo, mx], inp) => { | |
[_, m] = inp.match(/mask = ([01X]+)/) ?? []; | |
if (m) { | |
mo = BigInt('0b' + m.replaceAll('X', 0)); | |
ma = BigInt('0b' + m.replaceAll(0, 1).replaceAll('X', 0)); | |
mx = ((1n << 36n) - 1n) ^ ma; | |
} else { | |
[_, pos, val] = inp.match(/mem\[(\d+)\] = (\d+)/); | |
pos = ma & BigInt(pos) | mo, val = BigInt(val); | |
for (i = mx; i > 0n; i = (i - 1n) & mx) { | |
mem.set(pos | i, val); | |
} | |
mem.set(pos, val); | |
} | |
return [mem, ma, mo, mx]; | |
}, [new Map()])[0].values()) | |
.reduce((acc, val) => acc + val) |
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
a = document.querySelector('.puzzle-input').textContent.split(',').map(x => Number(x)); | |
m1 = new Map(), m2 = new Map(); | |
set = (x, i) => (m2.set(x, m1.get(x)), m1.set(x, i)); | |
get = (x) => m1.get(x) - (m2.get(x) ?? m1.get(x)); | |
for (i = 0; i < 2020; i++) { | |
if (i < a.length) { | |
set(a[i], i); | |
prev = a[i]; | |
continue; | |
} | |
age = get(prev); | |
set(age, i); | |
prev = age; | |
} | |
prev |
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
a = document.querySelector('.puzzle-input').textContent.split(',').map(x => Number(x)); | |
m1 = new Map(), m2 = new Map(); | |
set = (x, i) => (m2.set(x, m1.get(x)), m1.set(x, i)); | |
get = (x) => m1.get(x) - (m2.get(x) ?? m1.get(x)); | |
for (i = 0; i < 30000000; i++) { | |
if (i < a.length) { | |
set(a[i], i); | |
prev = a[i]; | |
continue; | |
} | |
age = get(prev); | |
set(age, i); | |
prev = age; | |
} | |
prev |
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
[spec, my, rest] = document.body.textContent.trim().split('\n\n'); | |
anyOk = spec.split('\n') | |
.flatMap(x => [...x.matchAll(/(\d+)-(\d+)/g)].map(([_, p, q]) => (z) => Number(p) <= z && z <= Number(q))) | |
.reduce((acc, val) => (z) => acc(z) || val(z)); | |
rest.split('\n').slice(1) | |
.flatMap(x => x.split(',').map(x => Number(x)).map(x => anyOk(x) ? 0 : x)) | |
.reduce((acc, val) => acc + val) |
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
[spec, my, rest] = document.body.textContent.trim().split('\n\n').map(x => x.split('\n')); | |
ok = spec.map(x => [...x.matchAll(/(\d+)-(\d+)/g)] | |
.map(([_, p, q]) => (z) => Number(p) <= z && z <= Number(q)) | |
.reduce((acc, val) => (z) => acc(z) || val(z))); | |
my = my[1].split(',').map(x => Number(x)); | |
rows = [my, ...rest.slice(1) | |
.map(x => x.split(',').map(x => Number(x))) | |
.filter(x => x.reduce((acc, val) => acc && ok.reduce((acc1, f) => acc1 || f(val), false), true))]; | |
g = ok.map(f => ok.flatMap((_, i) => rows.map(r => r[i]).reduce((acc, val) => acc && f(val), true) ? [i] : [])); | |
res = []; | |
g.forEach(_ => g.forEach((r, i) => { | |
if (r.length === 1) j = r[0], res[i] = j, g = g.map(r => r.filter(x => x != j)); | |
})); | |
spec.reduce((acc, x, i) => x.startsWith('departure') ? acc * my[res[i]] : acc, 1) |
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
g = document.body.textContent.split('\n').flatMap((r, i) => [...r].flatMap((x, j) => x === '#' ? [[0, i, j]] : [])); | |
sgfy = x => JSON.stringify(x); | |
nbs = ([x0, y0, z0]) => (d = [-1, 0, 1], d.flatMap(x => | |
d.flatMap(y => | |
d.flatMap(z => x === 0 && y === 0 && z === 0 ? | |
[] : [[x + x0, y + y0, z + z0]])))); | |
get = p => g.find(q => sgfy(p) === sgfy(q)); | |
keep = p => (cnt = nbs(p).filter(get).length, get(p) && cnt === 2 || cnt === 3); | |
for (t = 0; t < 6; t++) { | |
g = [...new Set([...g.map(sgfy), ...g.flatMap(nbs).map(sgfy)])].map(x => JSON.parse(x)).filter(keep); | |
} | |
g.length |
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
a = document.body.textContent.split('\n').flatMap((r, i) => [...r].flatMap((x, j) => x === '#' ? [[0, 0, i, j]] : [])); | |
sgfy = x => JSON.stringify(x), prse = x => JSON.parse(x); | |
cube = dims => dims === 0 ? [[]] : (m = cube(dims - 1), [-1, 1, 0].flatMap(d => m.map(a => [d, ...a]))); | |
cached = cube(a[0].length).slice(0, -1); | |
g = new Set(a.map(sgfy)); | |
nbs = p => cached.map(q => q.map((x, i) => x + p[i])); | |
get = p => g.has(p); | |
keep = p => (cnt = nbs(p).map(sgfy).filter(get).length, get(sgfy(p)) && cnt === 2 || cnt === 3); | |
for (t = 0; t < 6; t++) { | |
g = new Set([...new Set([...g, ...[...g].map(prse).flatMap(nbs).map(sgfy)])].map(prse).filter(keep).map(sgfy)); | |
} | |
g.size |
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
solve = a => { | |
st = [], ost = []; | |
pushop = () => st.push(eval(st.pop() + ost.pop() + st.pop())); | |
for (token of a) { | |
n = Number(token); | |
if (!isNaN(n)) { | |
st.push(n); | |
} else if (token === '(') { | |
ost.push(token); | |
} else if (token === ')') { | |
while (ost[ost.length - 1] !== '(') pushop(); | |
ost.pop(); | |
} else { | |
while (ost.length && ost[ost.length - 1] !== '(') pushop(); | |
ost.push(token); | |
} | |
} | |
while (ost.length) pushop(); | |
return st[0]; | |
} | |
document.body.textContent.split('\n') | |
.map(x => x.replaceAll('(', '( ').replaceAll(')', ' )').split(' ')) | |
.map(solve) | |
.reduce((acc, val) => acc + val) |
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
solve = a => { | |
st = [], ost = []; | |
pushop = o => st.push(eval(st.pop() + ost.pop() + st.pop())); | |
for (token of a) { | |
n = Number(token); | |
if (!isNaN(n)) { | |
st.push(n); | |
} else if (token === '(') { | |
ost.push(token); | |
} else if (token === ')') { | |
while (ost[ost.length - 1] !== '(') pushop(); | |
ost.pop(); | |
} else { | |
stop = token === '+' ? '*(' : '('; | |
while (ost.length && !stop.includes(ost[ost.length - 1])) pushop(); | |
ost.push(token); | |
} | |
} | |
while (ost.length) pushop(); | |
return st[0]; | |
} | |
document.body.textContent.split('\n') | |
.map(x => x.replaceAll('(', '( ').replaceAll(')', ' )').split(' ')) | |
.map(solve) | |
.reduce((acc, val) => acc + val) |
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
[rules, input] = document.body.textContent.split('\n\n').map(x => x.split('\n')); | |
rulesMap = new Map(rules.map(x => x.split(': ')) | |
.map(x => [x[0], x[1].startsWith('"') ? x[1].slice(1, -1) : x[1].split(' | ').map(y => y.split(' '))])); | |
makeRegex = i => { | |
rule = rulesMap.get(i); | |
if (typeof rule === 'string') return rule; | |
return `(${rule.map(x => x.map(makeRegex).join('')).join('|')})`; | |
}; | |
r = new RegExp(`^${makeRegex('0')}$`); | |
input.filter(x => r.test(x)).length |
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
[rules, input] = document.body.textContent.split('\n\n').map(x => x.split('\n')); | |
rulesMap = new Map(rules.map(x => x.split(': ')) | |
.map(x => [x[0], x[1].startsWith('"') ? x[1].slice(1, -1) : x[1].split(' | ').map(y => y.split(' '))])); | |
makeRegex = i => { | |
if (i === '8') return `${makeRegex('42')}+`; | |
if (i === '11') { | |
a = makeRegex('42'), b = makeRegex('31'); | |
maxlen = input.reduce((acc, val) => Math.max(acc, val.length), 0); | |
return `(${[...new Array(maxlen).keys()].map(i => `(${a}{${i + 1}}${b}{${i + 1}})`).join('|')})`; | |
} | |
rule = rulesMap.get(i); | |
if (typeof rule === 'string') return rule; | |
return `(${rule.map(x => x.map(makeRegex).join('')).join('|')})`; | |
}; | |
r = new RegExp(`^${makeRegex('0')}$`); | |
input.filter(x => r.test(x)).length |
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
borders = g => { | |
r = [g[0], g[g.length - 1], g.map((_, i) => g[i][0]).join(''), g.map((_, i) => g[i][g[i].length - 1]).join('')]; | |
return r.concat(r.map(x => [...x].reverse().join(''))); | |
} | |
document.body.textContent.trim().split('\n\n') | |
.map(x => x.split('\n')) | |
.map(x => ({id: Number(x[0].match(/\d+/)[0]), g: x.slice(1), borders: borders(x.slice(1))})) | |
.filter((x, _, a) => x.borders.map(b => a.filter(y => y.borders.includes(b)).length).filter(c => c === 1).length === 4) | |
.reduce((acc, x) => acc * x.id, 1) |
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
rot90 = g => g[0].map((_, i) => g.map(r => r[i]).reverse()) | |
lrflip = g => g.map(r => r.slice().reverse()); | |
variations = g => { | |
r0 = [g, lrflip(g)]; | |
r1 = r0.map(rot90); | |
r2 = r1.map(rot90); | |
r3 = r2.map(rot90); | |
return r0.concat(r1, r2, r3); | |
}; | |
topBorder = g => g[0].join(''); | |
bottomBorder = g => g[g.length - 1].join(''); | |
leftBorder = g => g.map(r => r[0]).join(''); | |
rightBorder = g => g.map(r => r[r.length - 1]).join(''); | |
trimBorders = g => g.slice(1, -1).map(r => r.slice(1, -1)); | |
a = document.body.textContent.trim().split('\n\n') | |
.map(x => x.split('\n')) | |
.map(x => ({id: Number(x[0].match(/\d+/)[0]), v: variations(x.slice(1).map(r => r.split('')))})); | |
findMatch = (b, tf, z) => z.map(t => [t, t.v.filter(g => b === tf(g))[0]]).filter(x => x[1]); | |
gg = []; | |
populate = (g, i, j) => { | |
for (ii = 0; ii < g.length; ii++) { | |
gg[ii + i] = gg[ii + i] ?? []; | |
gg[ii + i].splice(j, 0, ...g[ii]); | |
} | |
} | |
fillCol = (top, i, j) => { | |
m = findMatch(top, topBorder, [...s])[0]; | |
if (!m) return; | |
[t, last] = m; | |
s.delete(t); | |
g = trimBorders(last); | |
populate(g, i, j); | |
fillCol(bottomBorder(last), i + g.length, j); | |
} | |
fillRow = (left, i, j) => { | |
m = findMatch(left, leftBorder, [...s])[0]; | |
if (!m) return; | |
[_, head] = m; | |
fillCol(topBorder(head), i, j); | |
fillRow(rightBorder(head), i, j + trimBorders(head)[0].length); | |
} | |
corners = a.filter(t => t.v.filter(g => findMatch(topBorder(g), topBorder, a.filter(u => t !== u)).length).length === 4) | |
s = new Set(a); | |
ss = [...s].filter(t => t !== corners[0]); | |
last = corners[0].v.filter(g => findMatch(rightBorder(g), leftBorder, ss).length && findMatch(bottomBorder(g), topBorder, ss).length)[0]; | |
fillRow(leftBorder(last), 0, 0); | |
monster = ` # | |
# ## ## ### | |
# # # # # # `.split('\n').flatMap((r, i) => [...r].flatMap((c, j) => c === '#' ? [[i, j]] : [])); | |
isMonster = (g, i, j) => monster.every(([x, y]) => g[i + x] && g[i + x][j + y] === '#'); | |
countMonsters = g => g.flatMap((_, i) => g[0].map((_, j) => isMonster(g, i, j))).filter(x => x).length; | |
monsters = variations(gg).map(countMonsters).filter(x => x)[0]; | |
String(gg).split('#').length - 1 - monsters * monster.length |
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
a = document.body.textContent.trim().split('\n') | |
.map(x => x.slice(0, -1).split(' (contains ')) | |
.map(x => ({ing: x[0].split(' '), all: x[1].split(', ')})); | |
bad = a.flatMap(x => x.all).flatMap(al => a.filter(x => x.all.includes(al)) | |
.map(x => x.ing) | |
.reduce((acc, val) => acc.filter(i => val.includes(i)))); | |
a.flatMap(x => x.ing).filter(i => !bad.includes(i)).length |
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
a = document.body.textContent.trim().split('\n') | |
.map(x => x.slice(0, -1).split(' (contains ')) | |
.map(x => ({ing: x[0].split(' '), all: x[1].split(', ')})); | |
bad = [...new Set(a.flatMap(x => x.all))].sort().map(al => a.filter(x => x.all.includes(al)) | |
.map(x => x.ing) | |
.reduce((acc, val) => acc.filter(i => val.includes(i)))); | |
ans = []; | |
bad.forEach(() => bad.forEach((b, i) => { if (b.length === 1) ans[i] = b[0], bad = bad.map(b => b.filter(x => x !== ans[i])); })); | |
ans.join(',') |
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
[a, b] = document.body.textContent.trim().split('\n\n').map(x => x.split('\n').slice(1).map(x => Number(x))); | |
while (a.length && b.length) { | |
p = a.shift(), q = b.shift(); | |
if (p > q) a.push(p, q); | |
else b.push(q, p); | |
} | |
(a.length ? a : b).reverse().reduce((acc, val, i) => acc + val * (i + 1), 0) |
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
[a, b] = document.body.textContent.trim().split('\n\n').map(x => x.split('\n').slice(1).map(x => Number(x))); | |
play = (a, b) => { | |
let seen = new Set(); | |
while (a.length && b.length) { | |
key = String(a) + '/' + String(b); | |
if (seen.has(key)) break; | |
seen.add(key); | |
let p = a.shift(), q = b.shift(); | |
awin = a.length >= p && b.length >= q ? play(a.slice(0, p), b.slice(0, q)) : p > q; | |
if (awin) a.push(p, q); | |
else b.push(q, p); | |
} | |
return a.length; | |
} | |
(play(a, b) ? a : b).reverse().reduce((acc, val, i) => acc + val * (i + 1), 0) |
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
a = [...document.querySelector('.puzzle-input').textContent].map(x => Number(x)); | |
for (i = 0; i < 100; i++) { | |
x = a.shift(); | |
y = a.splice(0, 3); | |
r = a.filter(z => z < x); | |
if (!r.length) r = a; | |
z = r.reduce((acc, val) => Math.max(acc, val)); | |
a.splice(a.indexOf(z) + 1, 0, ...y); | |
a.push(x); | |
} | |
o = a.indexOf(1); | |
[...a.slice(o + 1), ...a.slice(0, o)].join('') |
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
a = [...document.querySelector('.puzzle-input').textContent].map(x => Number(x)); | |
cups = 1e6; | |
nxt = []; | |
for (i = 0; i < a.length - 1; i++) nxt[a[i]] = a[i + 1]; | |
nxt[a[a.length - 1]] = a.length + 1; | |
for (j = a.length + 1; j < cups; j++) nxt[j] = j + 1; | |
nxt[cups] = a[0]; | |
for (i = 0, cur = a[0]; i < 1e7; i++, cur = nxt[cur]) { | |
p = nxt[cur], q = nxt[p], r = nxt[q]; | |
t = cur - 1; | |
while ([p, q, r].includes(t)) t--; | |
if (!t) t = cups; | |
nxt[cur] = nxt[r], nxt[r] = nxt[t], nxt[t] = p; | |
} | |
nxt[1] * nxt[nxt[1]] |
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
dirs = {e: 1, w: -1, ne: 1000, nw: 999, se: -999, sw: -1000}; | |
document.body.textContent.trim().split('\n') | |
.map(x => x.match(new RegExp(Object.keys(dirs).join('|'), 'g')).map(x => dirs[x]).reduce((acc, val) => acc + val)) | |
.reduce((acc, val) => acc.has(val) ? (acc.delete(val), acc) : acc.add(val), new Set()) | |
.size |
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
dirs = {e: 1, w: -1, ne: 1000, nw: 999, se: -999, sw: -1000}; | |
a = document.body.textContent.trim().split('\n') | |
.map(x => x.match(new RegExp(Object.keys(dirs).join('|'), 'g')).map(x => dirs[x]).reduce((acc, val) => acc + val)) | |
.reduce((acc, val) => acc.has(val) ? (acc.delete(val), acc) : acc.add(val), new Set()); | |
nbs = x => Object.values(dirs).map(d => x + d); | |
keep = x => (cnt = nbs(x).filter(x => a.has(x)).length, a.has(x) && cnt === 1 || cnt === 2); | |
for (t = 0; t < 100; t++) { | |
a = new Set([...new Set([...a, ...[...a].flatMap(nbs)])].filter(keep)); | |
} | |
a.size |
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
[a, b] = document.body.textContent.trim().split('\n').map(x => Number(x)); | |
for (loops = 1, c = 7; c != b; loops++, c = c * 7 % 20201227) {} | |
for (c = 1; loops; loops--) c = c * a % 20201227; | |
c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment