Skip to content

Instantly share code, notes, and snippets.

@jsomers
jsomers / dice.py
Created October 11, 2013 03:11
A better method of generating Yahtzee-ish dice possibilities in Python
from itertools import product
def iterate_dice_simple(diceNums, diceToHold):
roles = list(product(range(1, 7), repeat=len(diceNums)))
return [r for r in roles if has_held_dice(r, diceNums, diceToHold)]
def has_held_dice(r, diceNums, diceToHold):
for i in range(len(diceToHold)):
if diceToHold[i] == 0: continue
if r[i] != diceNums[i]: return False
@jsomers
jsomers / menke.rb
Last active December 11, 2015 00:59
Solves the path-of-primes puzzle at http://www.sporcle.com/games/smenks13/path-of-primes.
#!/Users/jsomers/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
require 'prime'
class Menke
def initialize(numbers)
@numbers = numbers
@grid = make_grid
end
def solve!
@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}
$rom_counter = 0
$ram_counter = 16
$symbols = {
"SP" => "0",
"LCL" => "1",
"ARG" => "10",
"THIS" => "11",
"THAT" => "100",
"R0" => "0",
"R1" => "1",
@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>
# 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 / pe-215.rb
Created July 13, 2011 22:17
Solution to Project Euler problem #215
=begin
Most of the work here is in generating a sort of matrix of mutually
compatible "p-bricksets," or single-row arrangements of bricks. There are
something like 3,300 of these total.
Once you have that matrix - what in the code I call the "compatimap" - all you
do is exhaustively "expand" each p-brickset ten times (for the ten rows).
It helps to imagine the space of possible walls as a tree in which nodes are
@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('')
@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 / 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