This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#簡素版 | |
class UnionFind | |
def initialize(size) | |
@rank = Array.new(size, 0) | |
@parent = Array.new(size, &:itself) | |
end | |
def unite(x, y) | |
x = root(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'gtk2' | |
module LifeGame | |
class Field | |
MG = 4 | |
Small, Large = [70, 50], [85, 60] | |
def initialize | |
@width, @height = Small | |
@size = :small |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "curses" | |
class StarTrek | |
class DisplayIO | |
Width = Curses.cols | |
UpperH = 10 | |
def initialize | |
Curses.init_screen | |
Curses.noraw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Node = Struct.new(:key, :value) | |
class MaxHeap | |
def initialize | |
@heap = [nil] | |
@size = 0 | |
end | |
attr_reader :heap, :size | |
def insert(key, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'oekaki' | |
Oekaki.app width: 500, height: 500 do | |
draw { clear(color(0x1694, 0x3447, 0x8d60)) } | |
t = Oekaki::Turtle.new | |
t.color(0xfe5f, 0xaa9a, 0x212a) | |
ln = 10 | |
e = Enumerator.new do |y| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car | |
num = 1 | |
define_method(:initialize) do |wait_time:| | |
@num = num | |
num += 1 | |
@wait_time = wait_time | |
end | |
attr_reader :num, :wait_time | |
NewerOlder