Skip to content

Instantly share code, notes, and snippets.

View ColinDKelley's full-sized avatar

Colin Kelley ColinDKelley

  • Invoca, Inc.
  • Santa Barbara, CA
View GitHub Profile
orig = File.read("King Bee emails from Rachel.txt")
cleaned = orig
.force_encoding('BINARY')
.gsub("\xCA".force_encoding('BINARY'), '')
.gsub(/\r+/, '; ')
.gsub("\"; <", "\" <")
.gsub(/; */, "\n")
.gsub(/^"/, '')
.gsub("\" ", ' <')
Race 5 horses. Take the fastest 3 and race them against 2 chosen from the remainder. Repeat until there are are no remaining.
The final 3 from the last race are the fastest of the group.
races = 1 + (25 - 5)/2 = 11

Solution to Three Gods Logic Puzzle

  1. Ask god A: "Would the other non-random god answer 'Ja' to the question 'Does the random god sit to your left1?'"

=> If the answer is 'Ja', then it's false (or random), therefore NonRandom1 = C; else it's true (or random), therefore NonRandom1 = B

  1. Ask gods[NonRandom1]: "Would the other non-random god answer 'Ja' to the question 'Does the random god sit to your left1?"
@ColinDKelley
ColinDKelley / prisoner_hat.md
Last active December 25, 2015 20:37
prisoner hat riddle

Answer

The tallest prisoner counts the white hats in front of him. If odd, he calls out "white"; if even he calls out "black".

Then each prisoner down the line makes the same calculation. If the color they compute is different from what was called out before them, they know their color is "white"; if it's the same, their color is "black". They call out their color.

Reasoning

tiles = {}
STDIN.readlines.map.with_index do |line, y|
x = 0
line.chomp.split(".").each do |segment|
unless segment.empty?
tile = [x, segment.size, segment[0]]
if value = tiles[tile]
tiles[tile] = [value.first, value.last + 1]
else
@ColinDKelley
ColinDKelley / example_usage.rb
Last active August 29, 2015 14:06
retry_on_exception
result =
retry_on_exception(Timeout::Error, total_tries: 2) do
http_fe.post(...)
end
@ColinDKelley
ColinDKelley / parse_fig_leaf.rb
Created August 15, 2014 23:30
Fig leaf to intercept parse with American date formats like 03/04/05
module ParseFigLeaf
def parse(*args)
arg0 = args.first
arg0.is_a?(String) && arg0 =~ /\A\d\d[-\/]\d\d[-\/]\d\d/ and raise "Ambiguous parse format in #{arg0.inspect}"
super
end
end
class << Date
prepend ParseFigLeaf
require 'mysql2'
module Invoca
module Mysql2AdapterKillOnTimeout
def kill(thread_id)
@connection.query("KILL #{thread_id}")
end
def execute(sql, name = nil)
@ColinDKelley
ColinDKelley / modified_singleton-example.rb
Created March 13, 2014 00:50
modified_singleton-example.rb
class Secrets
def initialize(secrets_path)
@hash = YAML.load_file(secrets_path)
end
def [](key)
@hash[key]
end
def set_instance
@ColinDKelley
ColinDKelley / parallel_sync-example.rb
Created March 13, 2014 00:26
parallel_sync-example.rb
responses =
ExceptionalSynchrony::ParallelSync.parallel(EventMachine) do |parallel|
messages.each do |message|
parallel.add { message.send }
end
end
responses.each { ... }