Skip to content

Instantly share code, notes, and snippets.

@tompng
tompng / pow_k_sum.rb
Created July 6, 2024 09:53
sum of pow k
require 'matrix'
def powsum_params(k)
Matrix[*(k+2).times.map{|a|
(k+2).times.map{|b|(a+1)**b}
}].lup.solve((k+2).times.map{|n|(1..n+1).sum{_1**k}}).to_a
end
def assign_params(params, x)
params.each_with_index.sum {|p,i|p*x**i}
end
require 'rbs'
require 'rbs/cli'
loader = RBS::CLI::LibraryOptions.new.loader
def loader.each_dir
@loaded_dirs ||= Set.new
super do |source, dir|
next if @loaded_dirs.include? dir
@loaded_dirs << dir
yield source, dir
def prime_test?(n, a)
a.pow(n-1,n)==1
end
def ruby_mark(n,m,x,y)
0.5*x.abs+y.abs<n && y>-m
end
def pattern(ow, oh, iw, ih)
((1-oh)..ow).map{|y|
@tompng
tompng / git_changes.rb
Created June 15, 2024 15:53
git log additions and deletions
username = '...'
commits = `git log | grep -n1 "#{username}"`.scan(/-commit ([0-9a-f]+)/).flatten
result = commits.map do |commit|
stat = `git show #{commit} --stat -- lib`
diff = stat[/(\d+) insertion/].to_i - stat[/(\d+) deletion/].to_i
[commit, diff]
end
add_deletes = commits.map do |commit|
stat = `git show #{commit} -- lib`
@tompng
tompng / map_edit.rb
Created May 28, 2024 16:13
input: white-black map, output: rk2024-color map
require 'chunky_png'
img = ChunkyPNG::Image.from_file('okinawa_ishigaki_small.png')
alphas = img.height.times.map do |y|
img.width.times.map do |x|
((img[x,y] >> 8) & 0xff) / 255.0
end
end
lines = Hash.new(0)
borders = Hash.new(0)
files_rbreadline = Dir.glob('rb-readline/lib/**/*.rb')
files_reline = Dir.glob('reline/lib/**/*.rb')
K = 123456789
P = (1 << 81) | 17
def hashes(text, n)
output = {}
hash = 0
hash2 = 0
@tompng
tompng / irbrc
Last active June 7, 2024 05:04
return if ENV['WITHOUT_IRBRC']
{ IRB: 'irb', Reline: 'reline', ReplTypeCompletor: 'repl_type_completor' }.each do |const_name, lib|
require lib
const = Object.const_get(const_name)
version = const::VERSION
dir = File.expand_path File.join(File.dirname(const.const_source_location(:VERSION).first), '../../')
if File.exist?(File.join(dir, '.git'))
Dir.chdir dir do
branch = `git branch`[/^\* .+/][2..]
%w@state=32;(st@=>s;at_exit{eval(
c=s*'')};s+=%w@ate..).each{|s|a=64.ch
r;t=((-11..11).map{|iy|((-22..22).map{|ix
|x,y=((ix+iy*2i)*(1i**(s/16r))).rect;[x.abs+(
y-5).abs,y+10].max<23.4??X:32.chr}*'').rstrip
require 'matrix'
module RegEqSolver
class RHS
alias ~ binding
public :~
end
[Integer, Float, Rational, Complex].each do |klass|
refine klass do
def ~
$rhs = self
@tompng
tompng / ansi_pbcopy.rb
Last active April 28, 2024 18:20
Copy ansi_colored_text or ruby_code as rich text
require 'open3'
# Usage: cat code.rb | ruby ansi_pbcopy.rb --size 64 --font monospace
FONT = ARGV[ARGV.index('--font') + 1] rescue 'courier'
SIZE = ARGV[ARGV.index('--size') + 1] rescue 16
COLORS = ARGV[ARGV.index('--color') + 1].split rescue %w[#000000 #A00000 #008000 #606000 #0000C0 #800080 #008080 #808080]
initial = { bg: 9, fg: 9, bold: false, italic: false, underline: false }
style = initial.dup