Skip to content

Instantly share code, notes, and snippets.

View sarahzrf's full-sized avatar

sarahzrf sarahzrf

View GitHub Profile
require 'json'
class Atom
attr_accessor :val, :prv, :nxt, :become
def initialize(val)
@val = val
end
def leftmost
require 'json'
class Atom
attr_accessor :val, :prv, :nxt, :become
def initialize(val)
@val = val
end
def leftmost
def bits(n)
[(n & 0b1000) >> 3,
(n & 0b0100) >> 2,
(n & 0b0010) >> 1,
n & 0b0001]
end
def from_bits(bs)
bs.each_with_index.sum {|b, i| b * (1 << (bs.length - i - 1))}
end
def bits(n)
[(n & 0b1000) >> 3,
(n & 0b0100) >> 2,
(n & 0b0010) >> 1,
n & 0b0001]
end
def from_bits(bs)
bs.each_with_index.sum {|b, i| b * (1 << (bs.length - i - 1))}
end
require 'set'
# i had to learn dijkstra's algo for this...
class Cell
def initialize(cave, x, y)
@cave = cave
@x = x
@y = y
end
require 'set'
# i had to learn dijkstra's algo for this...
class Cell
def initialize(cave, x, y)
@cave = cave
@x = x
@y = y
end
def expand(rules, dat)
next_dat = Hash.new(0)
dat.each do |(l, r), ct|
m = rules[[l, r]]
next_dat[[l, m]] += ct
next_dat[[m, r]] += ct
end
next_dat
end
def expand(rules, dat)
return to_enum(__method__, rules, dat) unless block_given?
dat.each_cons(2).each do |c|
yield c[0]
yield rules[c]
end
yield dat[-1]
end
rules = {}
def expand(rules, dat)
return to_enum(__method__, rules, dat) unless block_given?
dat.each_cons(2).each do |c|
yield c[0]
yield rules[c]
end
yield dat[-1]
end
rules = {}
require 'set'
def fold(line, val)
if val < line
val
elsif val > line
line - (val - line)
else
raise "dot on line!"
end