Skip to content

Instantly share code, notes, and snippets.

@obelisk68
obelisk68 / union_find1.rb
Last active April 18, 2023 06:35
Union Find
#簡素版
class UnionFind
def initialize(size)
@rank = Array.new(size, 0)
@parent = Array.new(size, &:itself)
end
def unite(x, y)
x = root(x)
@obelisk68
obelisk68 / another_sudoku_solver.rb
Last active March 25, 2022 04:21
数独ソルバー
module Sudoku
Size = 81
Length = 9
INDEXS = [0, 3, 6, 27, 30, 33, 54, 57, 60]
.map { |i| [i, i+1, i+2, i+9, i+10, i+ 11, i+18, i+19, i+20] }
class CertainMap
def initialize(ary)
@field = ary
@obelisk68
obelisk68 / sokoban_solver.rb
Last active January 24, 2021 14:29
倉庫番ソルバー(Brute Force)
module Sokoban
Pos = Struct.new(:x, :y) do
def dirs
[Pos.new(x + 1, y), Pos.new(x, y - 1),
Pos.new(x - 1, y), Pos.new(x, y + 1)]
end
def inspect = "<#{x},#{y}>"
def over_there(e) = Pos.new(2 * e.x - x, 2 * e.y - y)
def <=>(a) = [x, y] <=> [a.x, a.y]
@obelisk68
obelisk68 / startrek.rb
Last active October 23, 2020 02:04
StarTrek Game (Ruby)
require "curses"
class StarTrek
class DisplayIO
Width = Curses.cols
UpperH = 10
def initialize
Curses.init_screen
Curses.noraw
@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
@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 / file_search
Last active December 29, 2019 14:21
findコマンドのGUI化
@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 / tetris_for_Ruby2D.rb
Last active March 29, 2022 18:35
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 / 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