Skip to content

Instantly share code, notes, and snippets.

@obelisk68
obelisk68 / max_heap.rb
Last active May 17, 2020 00:42
二分ヒープの Ruby 実装
Node = Struct.new(:key, :value)
class MaxHeap
def initialize
@heap = [nil]
@size = 0
end
attr_reader :heap, :size
def insert(key, value)
#!/usr/bin/env ruby
require 'socket'
require 'thwait'
def file_send
host = ARGV[0]
q = Queue.new
30.times {q.push(:unlock)}
send_file = ->(name) {
@obelisk68
obelisk68 / file_transfer_for_nexus7.rb
Created January 10, 2019 12:35
Ruby 1.9.3 用ファイル転送プログラム
#!/usr/bin/env ruby
# encoding : utf-8
require 'socket'
require 'thwait'
def file_send
host = ARGV[0]
q = Queue.new
30.times {q.push(:unlock)}
@obelisk68
obelisk68 / operations_parser.rb
Last active May 27, 2019 09:30
四則演算のパーサー(Ruby)
def parse(text)
splited = text.scan(/[0-9\.]+|\+|\-|\*|\/|\(|\)|=/)
output = []
stack = []
a = nil
until (token = splited.shift) == "="
case token
when "(" then stack << token
when ")"
@obelisk68
obelisk68 / simulated_annealing.rb
Created February 14, 2019 14:18
「最短ヌクレオチド連鎖問題」焼きなまし法
def dist(a, b)
case
when a[1, 2] == b[0, 2] then 1
when a[-1] == b[0] then 2
else 3
end
end
L = 64
start_t = 171
@obelisk68
obelisk68 / tetris_for_Ruby2D.rb
Last active December 15, 2024 14:50
Ruby2D を使ったテトリス
require "ruby2d"
include Ruby2D::DSL
Wait = 18
class Tetromino
def initialize
@pat = Array.new(4)
pats = [["1111"], ["11", "11"], ["011", "110"], ["110", "011"],
["100", "111"], ["001", "111"], ["010", "111"]]
@obelisk68
obelisk68 / gray_scott_gtk.rb
Last active November 14, 2019 16:31
Gray_Scott を Gtk でアニメーション表示
require 'numo/narray'
require 'gtk3'
include Numo
SPACE_GRID_SIZE = 256
VISUALIZATION_STEP = 8
Dx = 0.01
Du = 2e-5
Dv = 1e-5
@obelisk68
obelisk68 / file_search
Last active December 29, 2019 14:21
findコマンドのGUI化
@obelisk68
obelisk68 / aoj_0192.rb
Last active April 27, 2020 23:40
AOJ 0192: Multistory Parking Lot
class Car
num = 1
define_method(:initialize) do |wait_time:|
@num = num
num += 1
@wait_time = wait_time
end
attr_reader :num, :wait_time
@obelisk68
obelisk68 / liar_another.rb
Last active May 4, 2020 05:13
嘘つき問題
imp = ->(p, q) {!p || q}
eqv = ->(p, q) {!(!p ^ !q)}
def output(say, cond)
trans = ->(ary) {"[#{ary.map {|f| f ? "T" : "F"}.join(" ")}]"}
puts [trans.(say), trans.(cond)].join(" ")
end
cond = Array.new(5)
result = (1 << 5) - 1