Golfed Codepen – 3D Spiral Thing
// sort of golfed version of https://www.instagram.com/p/C1uv6Kqv19T/
// by @mewtru
b = document.body
a = Object.assign
a(b.style, { background:'#000', color:'#fff'})
w = 'snippet.zone snippet.zone'.toUpperCase().split``
F = (n, O = 0, C, S, o, t) => {
b.innerHTML += `<div id=${n} style='position:absolute;left:50%;top:50%;translate:-50% -50%;width:100% text-align:center;white-space:nowrap'></div>`
w.map(l => this[n].innerHTML += `<span style='display:inline-block;margin-right:5px;font-size:28px'>${l}</span>`)
t = O
setInterval(_ => {
t += .005
;[...this[n].children].map((e, i) => {
T = t + i / 2.7
a(e.style, {
translate: `0 ${Math.sin(T) * 100}px`,
scale: Math.cos(T) * .5 + .5})
}, 16)
})
}
F('Z')
F('X', 3)
“Very cool” pen by Lucas Fernando that comes from @mewtru
I decided to do a speed-coded semi-golfed version… can definitely be way more golfed 😀
Other Gates from NAND
const nand = ([a, b]) => +!(a & b)
const not = ([a]) => nand([a, a])
const and = ([a, b]) => nand([nand([a, b]), nand([a, b])])
const or = ([a, b]) => nand([nand([a, a]), nand([b, b])])
const nor = ([a, b]) =>
nand([
nand([nand([a, a]), nand([b, b])]),
nand([nand([a, a]), nand([b, b])])
])
const xor = ([a, b]) =>
nand([
nand([a, nand([a, b])]),
nand([b, nand([a, b])])
])
const xnor = ([a, b]) =>
nand([
nand([nand([a, a]), nand([b, b])]),
nand([a, b])
])
const inputs = [
[0, 0],
[0, 1],
[1, 0],
[1, 1]
]
const testGate = ({ gate, truth, result }) => console.log(
gate + ' matches truth? ',
truth+'' === result+'' ?
'yes :D' : `nope :( ${truth} ${result}`
)
testGate({
gate: 'NAND',
truth: [1, 1, 1, 0],
result: inputs.map(nand)
})
testGate({
gate: 'NOT',
truth: [0, 1],
result: [[1], [0]].map(not)
})
testGate({
gate: 'AND',
truth: [0, 0, 0, 1],
result: inputs.map(and)
})
testGate({
gate: 'OR',
truth: [0, 1, 1, 1],
result: inputs.map(or)
})
testGate({
gate: 'NOR',
truth: [1, 0, 0, 0],
result: inputs.map(nor)
})
testGate({
gate: 'XOR',
truth: [0, 1, 1, 0],
result: inputs.map(xor)
})
testGate({
gate: 'XNOR',
truth: [1, 0, 0, 1],
result: inputs.map(xnor)
})
Use NAND to create a bunch of other gates 😀 – I used this wikipedia article for reference
Multiplicative Persistence
const multp = (val, count = 1, res) =>
(res = (val + '').split``
.reduce((a, b) => a * b, 1) + '').length > 1 ?
multp(res, count + 1) : count
console.log('test:', multp(2678789))
Started watching this youtube video from numberphile and instantly made this half-golfed thing
Found this:
f=n=>[n,...n>9?f(eval([...n+''].join`*`)):[]]
By Arnauld over at codegolf.stackexchange
will definitely remember: [...n+'']
Building Spikes Codegolf
f =
n=>` ^
/|\\
/.|.\\
/..|..\\
___`.replace(/.[_|^]./g,'$&'.repeat(n))+'____'
// test it out
document.body.innerHTML +=
`<pre>${f(1)}
${f(3)}
${f(4)}`
Great codegolf stackexchange answer from user tsh
Odd Gradient Notation
// "Being clever is not clever"
// -- Bjarne Stroustrup
D = document
ang = {
'|': 180,
'-': 90,
'\\': 135,
'/': 225
}
box = def => {
def = def.split(/\s+/)
form = def.length
I = i => parseInt(def[i], 16)
;[,,, _=>{x = y = I(0); w = h = I(1); c = def[2]},
_=>{x = I(0), y = I(1); w = h = I(2);c = def[3]},
_=>{x = I(0); y = I(1); w = I(2); h = I(3); c = def[4]}
][form]()
c = c.split``
ca = c[0]
ca = ca+ca+ca
cD = ang[c[1]]
cb = c[2]
cb = cb+cb+cb
D.body.appendChild(
D.createElement`div`
).style = `
position: absolute; left: ${x}px; top: ${y}px;
width: ${w}px; height: ${h}px;
background: linear-gradient(${cD}deg, #${ca}, #${cb})
`
}
const parse = prog => prog.trim()
.split(/\n+/m)
.map(line => box(line.trim()))
parse(`
0 64 0/f
64 64 0\\f
a0 f0 30 54 f\\0
0 6f 20 60 0-c
f 7f 20 60 0|c
1f 8f 30 30 c/0
`)
Just playing around… odd micro-gradient notation:
'0 64 0/f'
// x=0 y=0 width=0x64 height=0x64
// 0/f = gradient black to white top right to bottom left
'64 64 0\\f'
// x=0 y=0 width=0x64 height=0x64
// 0\\f = black to to white gradient top left to bottom right
'0 6f 20 60 0-c'
// x=0 y=0x6f width=0x20 height=0x60
// 0-c = gradient black to grey (#ccc) left to right
// etc... ( | ) is top to bottom grad