Skip to content

Instantly share code, notes, and snippets.

class Integer
def self.isqrt(n)
return 0 if n == 0
return 1 if n < 4
shift = [n.bit_length / 4, 1].max
x = Integer.isqrt(n >> (shift * 2))
x = (x << (shift - 1)) + (n >> (shift + 1)) / x
xx = x * x
while xx > n
xx -= 2 * x - 1
@tompng
tompng / wave.rb
Last active May 20, 2024 08:41
wave_quine
t=( s=" wav e=- >c{ eva l(c.split(
$/) .map{ (_1 *2)[/ #.+ #/] }.j
oin .de let e(' #'+ 32. chr ))} ;class<<wa
ve;de f+(c) =call(c); end;w ave
"). tr" #$/ ",+ ''; eval(t)+%~
s . s p l i t(Re g e x p . n e # s p =3 2. c h r ; q =
) . z i p ( [ 0,4,4 , 5 , 1 , 1, 3# w ( s p + ' + ')
3, 2 , 2 , 1 ,1, 1 , 4 , 1 , 4 ,1 , 3 , # , 3 , 4 , 4 ,
* _ 1 }) . join # 3 , 3 , 4 , 4 , 4 ,4 ] . m a p { s p
@tompng
tompng / capture.html
Last active May 20, 2024 08:41
rk wasm signage
<link href="https://fonts.googleapis.com/css2?family=Dela+Gothic+One&family=Montserrat:wght@400;500;600;700;800&family=Poppins:wght@400;800;900&display=swap" rel="stylesheet">
<style>canvas{background:white;width:100vw;}body{background:gray;margin:0;}</style>
<script src="/dist/browser.script.iife.js"></script>
<script type="text/ruby">
Time.singleton_class.prepend Module.new{
def now
$called = true
$time ||= 0.0
end
}
def big_divmod(numerator, denominator)
numerator_bits = numerator.bit_length
denominator_bits = denominator.bit_length
inv = 1
accuracy_bits = 1
target_bits = [numerator_bits - denominator_bits]
while target_bits[0] > 1
target_bits.unshift (target_bits[0] * 4 + 1)/ 7
end
accuracy_bits = target_bits.shift
@tompng
tompng / integer_dup.rb
Last active February 28, 2024 04:54
baseの違うbignumの比較
def string_radix(s)
if s.match?(/0x/i)
[16, 2]
elsif s.match?(/0o/i)
[8, 2]
elsif s.match?(/0b/i)
[2, 2]
elsif s.start_with?('0')
[8, 0]
else
@tompng
tompng / irb_theme_2024.rb
Last active May 20, 2024 08:41
IRB theme RubyKaigi2024
Reline::LineEditor.prepend Module.new {
eval <<~RUBY
def colorize_completion_dialog
#{(%W(d=@d ialogs[0]; (c=d&.cont ents
)&&(y=d.scroll _top ;f=R eline::Face[:c omplet
ion_di alog]; s=T ime .now.t o_f;c[ 0..]=c.i
nsta nce_ ev al{@o||=du p} .map {|t| e=t. inde
x(f[: en hanced]);x=0;l =t .gsub (/#e #[[^
m]*m/,' ') .chars.map {" #e[48;5 ;${e ==0?
@tompng
tompng / minsa.rb
Last active May 20, 2024 08:40
sixel-minsa
require_relative'six' 'el';eval(%W`include~
Math;puts"Usage:~ruby ~#{__FILE__}~[line_he
ight]~[width]~[seed]" ;line_height=ARGV.fir
st&.to_i||14;width=AR GV[1]&.to_i||400;seed
=ARGV[2]&.to_i||rand( 100000);p(seed:);sran
d~seed;$maxw=240;$mpa t=[[1,1,0],[2,2,1],[1
,3,1]][rand**5*3];ab= [0,1].map{s=4*rand(2+
$mpat[2]..4);r=l=[2*r and(2),s,2*s].sample;
l,r=[l,4*s].shuffle~i f~rand<0.2;[l+r+3*s,l
,s,r,_1]};if(sum=ab.s um{_1[0]})<200*rand;i
def sum_reg(a, b, fallback = nil)
fragments = (0..18).map do |sum|
".*\\k<#{a}>.{#{sum/2+5}}_.{#{sum-sum/2+5}}\\k<#{b}>.*-.{#{sum%10}}"
end
fragments << ".*-.{#{fallback}}" if fallback
"(?:#{fragments.join('|')})"
end
S = '9876543210___________0123456789-0123456789'
@tompng
tompng / line_editor_buffer.svg
Last active February 24, 2024 17:31
reline line_editor readme svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tompng
tompng / reline_debug_visualizer.rb
Last active February 9, 2024 19:10
Minimal terminal emulator that only supports Reline's escape sequences
require 'pty'
require 'io/console'
if ARGV.empty?
puts <<~EOS
Reline Visualizer (Minimal terminal emulator that only supports Reline's escape sequences)
Usage: ruby #{__FILE__} <command>'
ruby #{__FILE__} irb
ruby #{__FILE__} bash
ruby #{__FILE__} zsh
ruby #{__FILE__} ruby -I path/to/reline/lib -I path/to/irb/lib path/to/irb/exe/irb