Skip to content

Instantly share code, notes, and snippets.

@obelisk68
obelisk68 / q56a.rb
Last active April 18, 2018 02:37
公平に分けられたケーキ
#(x, y)のケーキを切ってできる全ての (面積, 切った長さの合計) のペアを返す
#ただし面積はここでカットした人の総計
def cut(x, y)
x, y = y, x if x < y
return @memo[[x, y]] if @memo.has_key?([x, y])
return {1 => 1} if x == 2 and y == 1
result = {}
#前の人がカットしたところから自分の分を求める
@obelisk68
obelisk68 / lsystem.rb
Last active March 22, 2018 05:00
L-system の実装
require 'oekaki'
class Lsystem
def initialize(width, height, title = "L-system")
@width = width
@height = height
@title = title
@procedure = ""
@command = {}
@rule = {}
@obelisk68
obelisk68 / n_queen.go
Last active April 17, 2018 19:57
15 queen
package main
import "fmt"
const num = 15
var count = 0
func get_space(field []int) [num]int {
result := [num]int{}
l := len(field)
for i, queen := range field {
@obelisk68
obelisk68 / kaki-lifegame.rb
Last active May 8, 2022 10:26
エディタ付きライフゲーム(Ruby)
require 'gtk2'
module LifeGame
class Field
MG = 4
Small, Large = [70, 50], [85, 60]
def initialize
@width, @height = Small
@size = :small
@obelisk68
obelisk68 / calender.rb
Last active April 12, 2018 04:49
Created by RubyPico at Thu Apr 12 12:29:20 2018
#for RubyPico (mruby)
#mrubyには String#center がないので適当に実装
class String
def center(n)
i = (n - length).div(2)
" " * i + self + " " * (n - i - length)
end
end
@obelisk68
obelisk68 / lifegame_for_RubyPico.rb
Last active April 12, 2018 16:27
RubyPico 用ライフゲーム
class LifeGame
W, H = 124, 38
def initialize(n)
@field = new_field
scatter(n)
end
def set_cell(x, y)
@field[y + 1][x + 1] = 1
end
@obelisk68
obelisk68 / linuxmint_backup.rb
Last active April 20, 2018 11:13
home フォルダのバックアップ
require 'fileutils'
class String
def cut
/.+tomoki\/(.+)$/.match(self)[1]
end
end
def get_file_names(f)
dirs = Dir.glob("*")
@obelisk68
obelisk68 / blackjack.rb
Last active March 17, 2019 12:46
カードゲーム「ブラックジャック」の実装
deck = nil #カードの山
shuffle = lambda do
print "カードをシャッフルします\n\n"
deck = 4.times.flat_map {|i| (1..13).map {|j| i * 100 + j} }.shuffle
end
# 一回の勝負
@obelisk68
obelisk68 / prime_table.rb
Last active April 30, 2018 01:42
省メモリで「エラトステネスの篩」同等の結果を得るコード
def prime_table(n)
return [] if n < 2
return [2] if n == 2
table = [2, 3]
i, step = 5, 2
while i <= n
guard = Math.sqrt(i).to_i
table.each do |prime|
break if (i % prime).zero?
if prime > guard
@obelisk68
obelisk68 / oekaki_sample19a.rb
Last active May 4, 2020 23:25
GTK+ で落書き 15
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|