Skip to content

Instantly share code, notes, and snippets.

@jsomers
jsomers / ghost.rb
Created April 24, 2010 23:35
Code to help analyze games of Ghost
Words = File.new("./pruned-words.txt").read.split("\n")
class String
def starts_with?(str)
self[0..str.length - 1] == str
end
end
# Players can never play "bones", e.g., since the game would end with "bone".
# The next two functions remove all 128,832 such extraneous words from our word list.
@jsomers
jsomers / mindy.rb
Created April 25, 2010 07:58
Functions for dealing and scoring games of Mindy Coat
# The Rules of Mindy Coat
# =======================
# => Four players, opposite sides are in teams.
# => Deal 5 cards each, one-by-one starting left of the dealer.
# => Left of the dealer calls trump looking at those 5.
# => Dealer deals the last 8 cards out (starts on the left).
# => Highest card of the trick wins, unless a trump is thrown, in which case the highest trump wins.
# => Team that takes the most tens wins, unless each takes two, in which case the total number of tricks is counted.
# => (Must follow suit unless you don't have it.)
@jsomers
jsomers / puzzles.rb
Created February 4, 2011 15:02
A script that finds unique word templates
words = File.open("./TWL06.txt").read.downcase.gsub("\r", "").split("\n")
words.select! {|w| w.length == 6}
# Approach:
# 1. Generate an index that maps 2-templates (like "_l___x") to words ("climax").
# 2. Find 2-templates that map to just one word.
def templates(word)
"Finds all fifteen (6! / 4! * 2!) 2-templates for a given word."
inds = []; (0..4).collect {|i| (i + 1..5).each {|j| inds << [i, j]}}
@jsomers
jsomers / collatz.rb
Created February 8, 2011 21:13
A solution to Project Euler problem #14
index = {}
def nxt(n)
(n % 2 == 0 ? n / 2 : 3 * n + 1)
end
(2..1_000_000).each do |n|
c = n + 0
ct = 0
while n > 1
@jsomers
jsomers / pe-191.rb
Created February 26, 2011 19:02
A solution to Project Euler problem #191
# [n, tot, -AA, 0Ls, -A, 1L-AA, 1L-A, 1LnotA]
lst = [1, 3, 0, 2, 1, 0, 0, 1]
while lst[0] < 30
n, t, a, b, c, d, e, f = lst
lst = [n + 1, 2 * t + b - a, c, 2 * b - a + d, t - (a + c), e, f, t]
end
p lst[1] # => 1918080160
@jsomers
jsomers / sporcle.rb
Last active September 25, 2015 08:18
complex sporcle-like word puzzles
N = 6
R = 2
def masks
@masks ||= (0..N - 1).to_a.combination(N - R).to_a
end
def templates(word)
masks.inject([]) do |templates, indexes_to_hide|
arr = word.split('')
# See http://www.glicko.net/glicko/glicko.pdf
DEFAULT_RATING = 1500.0
AVERAGE_RD = 50.0 # Could be determined empirically.
PERIODS_TO_RESET = 400.0 # If you sit out 400 games your rating is as uncertain as a new player's.
UNRATED_RD = 350.0
MINIMUM_RD = 30.0
C = Math::sqrt((UNRATED_RD ** 2 - AVERAGE_RD ** 2) / PERIODS_TO_RESET)
Q = 0.0057565
@jsomers
jsomers / ether.html
Created June 12, 2012 03:41
A minimal etherpad-like diff patch recorder (with playback)
<!-- TODOS
* More feedback about when the last save was, and what type it was.
* Word count responding to highlights initiated via the keyboard.
-->
<link type="text/css" href="./pad.css" rel="stylesheet" />
<script src="./diff_match_patch.js" type="text/javascript"></script>
<script src="./jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="./field-selection.js" type="text/javascript"></script>
$rom_counter = 0
$ram_counter = 16
$symbols = {
"SP" => "0",
"LCL" => "1",
"ARG" => "10",
"THIS" => "11",
"THAT" => "100",
"R0" => "0",
"R1" => "1",
@jsomers
jsomers / anagram_circles.rb
Last active October 13, 2015 18:27
Anagram circles
words = File.open("/usr/share/dict/words").read.split("\n").map(&:downcase)
anagram_index = words.inject(Hash.new {[]}) { |idx, word| idx[word.split("").sort] += [word]; idx }
p anagram_index.select {|_, v| v.length == 4}