Skip to content

Instantly share code, notes, and snippets.

View goofballLogic's full-sized avatar
👔
Still counting things

Andrew Stewart Gibson goofballLogic

👔
Still counting things
  • 10:11 (UTC +01:00)
View GitHub Profile
function resolvePath(fs, path) {
if (!(path in fs)) return null;
const { [path]: nextPath, ...others } = fs;
return nextPath ? resolvePath(others, nextPath) : path;
}
const resolvePath = (fs, path, ...visited) =>
visited.includes(path)
? null
: fs[path]
? resolvePath(fs, fs[path], path, ...visited)
: path
;
const swap = (xs, n, i) =>
xs.fill(
xs.splice(i+1, 1, n)[0],
i,
i+1
)
;
const chase = (xs, n, i) =>
i === xs.length - 1
// thanks: https://stackoverflow.com/a/16353241
const isLeapYear = year =>
(
// divisible by 4 but not 100
(year % 4 == 0) && (year % 100 != 0)
) || (
// divisible by 400
year % 400 == 0
);
const flippedy = str =>
str
?.split(" ")
.map(word => [
word.match(/[aeiou]/gi)?.length || 0,
word
])
.reduce((a, word, i) =>
i
? [
const hungryBears = bears =>
bears
.map(x => ({ ...x })) // avoid tainting the inputs
.sort((a, b) =>
(a.visited = a.visited || (bears.sum = (bears.sum || 0) + a.hunger) || true)
&&
(b.visited = b.visited || (bears.sum = (bears.sum || 0) + b.hunger) || true)
&&
(a.name > b.name ? 1 : a.name < b.name ? -1 : 0))
.filter(b => b.hunger > bears.sum / bears.length)
const replaceRepeats = (input, number) =>
input.replaceAll(
new RegExp(`${number}+`, "g"),
x => x.length);
// test
[
replaceRepeats('1234500362000440', 0) === "1234523623441",
replaceRepeats('000000000000', 0) == "12",
/* solution */
const validate = ([y, z, ...rest]) =>
rest.length
? (rest.shift() === y && validate([z, y, ...rest]))
: true;
/* testing */
/* solution */
function process([y, z, ...rest] = []) {
while (rest.length) {
if (rest.shift() !== y) return false;
[z, y] = [y, z];
}
return true;
}
const rotate = ([ head, ...tail]) => [...tail, head];
const explode = (num, acc = []) =>
num === 0 ? acc
: explode(num - 1, [num, ...acc])
;
const build = (num, input, lines = []) =>
num === 0 ? lines
: build(num - 1, rotate(input), [...lines, input])
;