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
  • 08:42 (UTC +01:00)
View GitHub Profile
@goofballLogic
goofballLogic / cassidoo_jun_29_2006.js
Last active June 29, 2026 12:26
not entirely sure i got the math spot on, and I'm fairly sure I can do this with a logarithm if I thought hard enough but...
function meanBits(n) {
let bits = 1, counted = 0, sum = 0;
while(counted < n) {
const depth = Math.pow(2, Math.max(1, bits - 1));
const count = Math.min(n - counted, depth);
sum += count * bits;
counted += count;
bits++;
}
// note that the routine would break for much of the unicode range
const latinWithPunctuation = /^[a-zA-Z\s.,\/#!$%\^&\*;:{}=\-_`~()?"'’\-]*$/;
const toggleCharCode = cc =>
(cc >= 97 && cc <= 122)
? cc - 32
: (cc >= 65 && cc <= 90)
? cc + 32
: cc;
const validatePizza = (layers, rules) =>
rules.find(([a, b]) => layers.indexOf(a) > layers.indexOf(b)) || true;
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",