Skip to content

Instantly share code, notes, and snippets.

### Keybase proof
I hereby claim:
* I am rampantmonkey on github.
* I am rampantmonkey (https://keybase.io/rampantmonkey) on keybase.
* I have a public key whose fingerprint is 35BC BE03 CA63 87B5 2349 5EE0 BAD7 5204 D119 CD2B
To claim this, I am signing this object:
@rampantmonkey
rampantmonkey / dice_rolling.rb
Created October 21, 2013 16:22
Dice rolling. Compute the probability of rolling n 2's with i dice.
#!/usr/bin/env ruby
class Die
def initialize sides=6
@sides = sides
end
def roll
Random.rand(1..@sides)
end
@rampantmonkey
rampantmonkey / answer.rb
Created September 26, 2013 18:48
International Trade as of 9/26
#!/usr/bin/env ruby
require_relative 'graph'
TRANSACTION_LOG = ARGV[0]
RATES_FILE = ARGV[1]
TARGET_ITEM = "DM1182"
TARGET_CURRENCY = :USD
Transaction = Struct.new :store, :sku, :amount
@rampantmonkey
rampantmonkey / ssh_test.exp
Created August 21, 2013 18:14
Test ssh access for a list of machines.
#!/usr/bin/expect
# Usage: ./ssh_test.exp MACHINE_NAME crobins9 ******
set timeout 7
set machine [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh -oStrictHostKeyChecking=no "$user@$machine"
expect {
@rampantmonkey
rampantmonkey / p28.rb
Last active December 20, 2015 19:58
Project Euler problem #28
#!/usr/bin/env ruby
class Spiral
def initialize
@diagonal_values = []
@current = 0
@spiral_count = 1
recompute_diagonals
end
@rampantmonkey
rampantmonkey / README.md
Created August 2, 2013 14:05
Draw a randomly colored box with `Tk` and `Thread`s.

During my code spike to explore Tk as a potential candidate for my Gameboy emulator screen I created this example. It uses threads to send events to the Tk main loop and consequently update the screen. Threads were important because I will need to trigger the screen update from other classes (specifically the cpu).

I chose to use the PPM format to store the image since it is easy to create and allows pixel manipulation. The downside is performance. The data to create the image is large (one byte per pixel). For the real thing I should be able to avoid memory allocation for every update by encapsulating the data in a class with helper methods for per pixel manipulation.

The update events must be sent from a different process. This will require forking and putting all of this code (including the require statements) inside of the child process and pulling update events from a pipe.

@rampantmonkey
rampantmonkey / p11.rb
Last active December 20, 2015 12:48
Project Euler Problem #11
#!/usr/bin/env ruby
input = ARGV[0]
class Grid
attr_reader :data
def initialize file, count=4
@data = IO.read(file).lines.map{|l| l.split " "}.map{|l| l.map{|i| i.to_i}}
@rampantmonkey
rampantmonkey / vertical_histogram.c
Last active December 11, 2015 03:39
Create a vertically oriented histogram based on the concepts introduced in chapter 1 of "The C Programming Language"
#include <stdio.h>
int main() {
const int MAX_LENGTH = 20;
int hist_data[MAX_LENGTH] = {0};
char c='a';
int current_length = 0;
while ((c = getchar()) != EOF) {