)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Golfed Codepen – 3D Spiral Thing

  1. // sort of golfed version of https://www.instagram.com/p/C1uv6Kqv19T/
  2. // by @mewtru
  3. b = document.body
  4. a = Object.assign
  5. a(b.style, { background:'#000', color:'#fff'})
  6. w = 'snippet.zone snippet.zone'.toUpperCase().split``
  7. F = (n, O = 0, C, S, o, t) => { 
  8.   b.innerHTML += `<div id=${n} style='position:absolute;left:50%;top:50%;translate:-50% -50%;width:100% text-align:center;white-space:nowrap'></div>`
  9.   w.map(l => this[n].innerHTML += `<span style='display:inline-block;margin-right:5px;font-size:28px'>${l}</span>`)
  10.   t = O
  11.   setInterval(_ => {
  12.     t += .005
  13.     ;[...this[n].children].map((e, i) => { 
  14.       T = t + i / 2.7
  15.       a(e.style, {
  16.         translate: `0 ${Math.sin(T) * 100}px`,
  17.         scale: Math.cos(T) * .5 + .5})
  18.     }, 16)
  19.   })
  20. }
  21. F('Z') 
  22. 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 😀

Faster than save/restore HTML5 canvas

  1. const canvas = document.createElement('canvas')
  2. const c = canvas.getContext('2d')
  3.  
  4. canvas.width = innerWidth
  5. canvas.height = innerHeight
  6.  
  7. document.body.append(canvas)
  8.  
  9. c.fillStyle = 'black'
  10. c.fillRect(0, 0, canvas.width, canvas.height)
  11.  
  12. class Shooter {
  13.   constructor() {
  14.     this.x = innerWidth / 2
  15.     this.y = innerHeight / 2
  16.     this.vx = Math.random() * 10 - 5
  17.     this.vy = Math.random() * 10 - 5
  18.     this.color = 'rgba(255, 0, 0, 0.5)'
  19.     this.size = 10
  20.     this.halfSize = this.size / 2
  21.   }
  22.   draw() {
  23.     this.x += this.vx
  24.     this.y += this.vy
  25.  
  26.     if (this.x < 0) {
  27.       this.x = innerWidth
  28.     } else if (this.x > innerWidth) {
  29.       this.x = 0
  30.     }
  31.  
  32.     if (this.y < 0) {
  33.       this.y = innerHeight
  34.     } else if (this.y > innerHeight) {
  35.       this.y = 0
  36.     }
  37.  
  38.     c.fillStyle = this.color
  39.     c.translate(this.x, this.y)
  40.     c.fillRect(-this.halfSize, -this.halfSize, this.size, this.size)
  41.  
  42.     c.setTransform(1, 0, 0, 1, 0, 0)
  43.   }
  44. }
  45.  
  46. const NUM = 1000
  47. const shooters = []
  48. for (let i = 0; i < NUM; i++) {
  49.   shooters.push(new Shooter())
  50. }
  51.  
  52. function loop() {
  53.   c.fillStyle = 'rgba(0, 0, 0, 0.1)'
  54.   c.fillRect(0, 0, innerWidth, innerHeight)
  55.  
  56.   for (let i = 0; i < NUM; i++) {
  57.     shooters[i].draw()
  58.   }
  59.   requestAnimationFrame(loop)
  60. }
  61. loop()

Using setTransform(1, 0, 0, 1, 0, 0) is faster than using save and restore. If you don’t need to save context info like fills, line styles etc… consider this method.

Little Trading Program

  1. let id = 0;
  2. function person(startValue = 100) {
  3.   const props = {
  4.     id: id++, 
  5.     el: document.body.appendChild(
  6.       Object.assign(
  7.         document.createElement`b`, { 
  8.           innerText: startValue,
  9.           style: 'padding: 1em'
  10.       })
  11.     ),
  12.     value: startValue,
  13.     trade(other) {
  14.       if (props.value <= 0) return;
  15.       other.value += 1;
  16.       props.value -= 1;
  17.       other.el.innerText = other.value
  18.       props.el.innerText = props.value
  19.     }
  20.   }
  21.   return props
  22. }
  23.  
  24.  
  25. const NUM = 4
  26. const people = Array.from({ length: NUM }, person)
  27.  
  28. setInterval(() => {
  29.   const a = people[~~(Math.random() * people.length)]
  30.   const b = people[~~(Math.random() * people.length)]
  31.  
  32.   if (a.id != b.id) a.trade(b)
  33. }, 1)

4 “people” randomly trade…

I asked Chat GPT to describe this… funny response…

The trading program you provided is essentially an example of a random walk or “drunkard’s walk” problem. In such problems, each individual (in this case, the ‘people’) make random moves (in this case, trades), and we observe the resulting dynamics of the system as a whole. In this particular case, each trade involves one individual giving one unit of value to another individual. Since each trade is random, there’s a possibility that over time, all the value might get concentrated in one individual. This isn’t guaranteed, but given enough time, it’s likely. You could compare it to the idea that given enough time, a group of monkeys randomly typing on a keyboard would eventually type out the complete works of Shakespeare. It’s also worth noting that this concentration of value becomes more likely because the individuals with no value left cannot trade anymore, so they are essentially removed from the trading pool. This progressively reduces the number of individuals that can gain value and increases the probability of the remaining individuals gaining more value. This is a simple model of economic transactions and it illustrates how wealth can concentrate over time even if all the transactions are fair and random. Of course, it’s a very simplified model, and real-world economics are much more complex.

Other Gates from NAND

  1. const nand = ([a, b]) => +!(a & b)
  2.  
  3. const not = ([a]) => nand([a, a])
  4.  
  5. const and = ([a, b]) => nand([nand([a, b]), nand([a, b])])
  6.  
  7. const or = ([a, b]) => nand([nand([a, a]), nand([b, b])])
  8.  
  9. const nor = ([a, b]) => 
  10.   nand([ 
  11.     nand([nand([a, a]), nand([b, b])]), 
  12.     nand([nand([a, a]), nand([b, b])])
  13.   ])
  14.  
  15. const xor = ([a, b]) =>
  16.   nand([
  17.     nand([a, nand([a, b])]),
  18.     nand([b, nand([a, b])])
  19.   ])
  20.  
  21. const xnor = ([a, b]) => 
  22.   nand([ 
  23.     nand([nand([a, a]), nand([b, b])]),
  24.     nand([a, b])
  25.   ])
  26.  
  27.  
  28. const inputs = [
  29.   [0, 0],
  30.   [0, 1],
  31.   [1, 0],
  32.   [1, 1]
  33. ]
  34.  
  35. const testGate = ({ gate, truth, result }) => console.log(
  36.   gate + ' matches truth? ', 
  37.   truth+'' === result+'' ? 
  38.     'yes :D' : `nope :( ${truth} ${result}`
  39. )
  40.  
  41. testGate({
  42.   gate: 'NAND',
  43.   truth: [1, 1, 1, 0],
  44.   result: inputs.map(nand)
  45. })
  46.  
  47. testGate({
  48.   gate: 'NOT',
  49.   truth: [0, 1],
  50.   result: [[1], [0]].map(not)
  51. })
  52.  
  53. testGate({
  54.   gate: 'AND',
  55.   truth: [0, 0, 0, 1],
  56.   result: inputs.map(and)
  57. })
  58.  
  59. testGate({
  60.   gate: 'OR',
  61.   truth: [0, 1, 1, 1],
  62.   result: inputs.map(or)
  63. })
  64.  
  65. testGate({
  66.   gate: 'NOR',
  67.   truth: [1, 0, 0, 0],
  68.   result: inputs.map(nor)
  69. })
  70.  
  71. testGate({
  72.   gate: 'XOR',
  73.   truth: [0, 1, 1, 0],
  74.   result: inputs.map(xor)
  75. })
  76.  
  77. testGate({
  78.   gate: 'XNOR',
  79.   truth: [1, 0, 0, 1],
  80.   result: inputs.map(xnor)
  81. })

Use NAND to create a bunch of other gates 😀 – I used this wikipedia article for reference

Multiplicative Persistence

  1. const multp = (val, count = 1, res) => 
  2.   (res = (val + '').split``
  3.     .reduce((a, b) => a * b, 1) + '').length > 1 ?
  4.       multp(res, count + 1) : count
  5.  
  6.  
  7. 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+'']

// golfed // hacks // humor // javascript // math
snippet.zone ~ 2021-24 /// {s/z}