Skip to content

Instantly share code, notes, and snippets.

@tompng
tompng / visualizer.rb
Created April 29, 2023 12:41
CUI rendering visualiser
View visualizer.rb
require 'io/console'
require 'pty'
require 'optparse'
opt = OptionParser.new
opt.on('--theme COLOR_THEME') do |theme|
raise 'Invalid theme: theme should be `dark` or `light`' unless %w[dark light].include? v
$theme = theme.to_sym
end
opt.on('--dark') { $theme = :dark }
opt.on('--light') { $theme = :light }
View irb_slowmotion.rb
require 'io/console'
require 'pty'
STDIN.raw!
$> << "\e[H\e[2J\e[2;1H"
row, col = 1, 1
width = 100
height = 40
$write_sleep = 0
$show_sleep = 0
$darkness = false
View bitmap_convert.rb
require 'chunky_png' # gem install chunky_png
file = ARGV[0]
level = ARGV[1]&.to_i || 5
NPIXEL = 16
size = NPIXEL * 2 ** level
p size
def scale(img, size)
w = img.width
h = img.height
@tompng
tompng / colorize.rb
Last active February 18, 2023 09:26
福岡rubyist会議Quine
View colorize.rb
require'irb/color'
code = File.read(ARGV[0] || 'fukuokark.rb')
def hoge(s)
s.gsub("eval(s*'')","$a=s*''")
end
eval code.gsub('then{;eval', 'then{eval hoge')
w = 132
ended = false
colored = IRB::Color.colorize_code(code.lines[0].dup+'%').gsub(/\n.+/,"\n")
View slow_irb_emulate.rb
require'pty'
require'io/console'
PTY.spawn('irb'){|r,w|
echo = true
q = Queue.new
Thread.new{
loop{
if echo != w.echo?
echo = w.echo?
View heartbeat.rb
# rubocop:disable all
q=->{!sleep _1/1e2}
p=(c=0..2).map{[_1/9r,0,5**_1.i/3,1,0]}
require'socket'
puts'op'"en http://localhost:#{(w=TCPServer.new$*[0]||0).addr[1]}"
Thread.new{
q[2]
f=[-1]*s=3
t=Time.now.to_f
p.select!{0<_1[3]=[_1[3]+_1[4]/8.0,1].min}
View okinawa.rb
code=%(h=60;w=140;s
=32.chr;q=s+'rq_xkyzsugt[w
vc'.bytes.map{(_1+9509).chr'u
tf-8'}*'';l=(s*22+'code=%('+code
+");eval(code.scan(/[!-~]/)*'');") ▄▄
.tr(q,s).split($/);v=40.times.map{[ ▟███▄
2*rand-1,rand,0.2+rand,1+_1*0.05]};(0 ▐██████▄
..).each{|t|cv=(1..h).map{[0]*w};v.eac ▐████████▖
h{->cx,cy,r{((2*cx-2*r).ceil..2*cx+2*r ▝█████████▖
).map{|x|((cy-r).ceil..cy+r).map{|y|(2* ▝█████████▖
View matrix_mult_concat.rb
# https://twitter.com/potetoichiro/status/1538130783785095173 を見て遊んでみた
hoge=(11..99).select{_1%10!=0}.to_h do |num|
a, b = num.divmod 10
nums = (1..9).to_a
combinations = nums.repeated_permutation(4).select do |a2, a3, b2, b3|
num == a * b + a2 * b2 + a3 * b3
end
[num, combinations.map{|a2,a3,b2,b3|[[a,a2,a3],[b,b2,b3]]}]
end
View TupleMap.ts
class TupleMap<Key extends readonly any[], Value> {
identifierMap = new Map<any, [number, number]>()
internalEntries = new Map<string, readonly [Readonly<Key>, Value]>()
lastId = 0
addKey(key: Readonly<Key>) {
const { identifierMap } = this
return key.map(item => {
let entry = identifierMap.get(item)
if (!entry) identifierMap.set(item, entry = [this.lastId++, 0])
entry[1]++
@tompng
tompng / fact.rb
Last active May 19, 2022 12:42
expression and thresholds for factorial
View fact.rb
def fact_i(n) = n == 0 ? 1 : (1..n).inject(:*)
def fact_s(x)
x+=1
Math.sqrt(2*Math::PI/x)*(x/Math::E)**x*Math.exp(
1/12.0/x-1/360.0/x**3+1/1260.0/x**5-1/1680.0/x**7+1/1188.0/x**9
)
end
def fact_i2(x)