Skip to content

Instantly share code, notes, and snippets.

View willhbr's full-sized avatar

Will Richardson willhbr

View GitHub Profile
read("", :program, {
def(:program_length, send(program, :length))
def(:program_location, 0)
def(:input, (1, 2, 3, 4))
def(:input_location, 0)
def(:program_char, {
send(program, :substring, program_location, +(program_location, 1))
})
@willhbr
willhbr / oh_god_why.swift
Last active May 7, 2016 05:28
I hope you're happy
/// Please don't do this
func iff(condition: Bool, block: () -> ()) -> Bool {
if(condition) {
block()
}
return condition
}
func iff<T>(optional: Optional<T>, block: (val: T) -> ()) -> Bool {
@willhbr
willhbr / matrix.py
Created January 31, 2016 05:15
Matrix Determinant Calculator
def calculate(mat, mul=1):
return (mul * mat[0][0] if len(mat) == 1 else sum((mul * calculate(list(map(lambda j: list(filter(lambda el: el != None, (mat[j][k] if k != i else None for k in range(len(mat))))), range(1, len(mat)))), (-1 if i % 2 == 0 else 1) * mat[0][i])) for i in range(len(mat))))
matrix = [
[1, -2, 3],
[0, -3, -4],
[0, 0, -3]
]
print(calculate(matrix))
@willhbr
willhbr / GrahamScan.swift
Created January 31, 2016 04:55
Graham Scan in Swift
struct Point: CustomStringConvertible {
let x: Int
let y: Int
func theta(anchor: Point) -> Float {
let dx = Float(x - anchor.x)
let dy = Float(y - anchor.y)
let t = dy / (abs(dx) + abs(dy))
if dx == 0 && dy == 0 {
return 0
@willhbr
willhbr / pretty_json.rb
Created October 25, 2015 22:02
Format JSON with newlines.
def self.pretty_json(o, l=0)
o.class == Hash ? "{\n" + o.keys.map { |k| (" " * (l + 1) * INDENT) + "\"#{k.to_s}\": " + pretty_json(o[k], l + 1) }.join(",\n") + "\n" + (" " * l * INDENT) + "}" : o.class == Array ? "[\n" + o.map { |k| (" " * (l + 1) * INDENT) + pretty_json(k, l + 1) }.join(",\n") + "\n" + (" " * l * INDENT) + "]" : o.inspect
end
@willhbr
willhbr / graham_scan.hs
Created July 4, 2015 13:47
Graham Scan Implementation in Haskell
import Data.List
import Data.List.Split
import Data.Ord
import System.Environment
theta :: (Num a, Eq a, Ord a, Fractional a) => (a,a) -> (a,a) -> a
theta (x1,y1) (x2,y2)
| dx == 0 && dy == 0 = 0
| dx < 0 = 2 - t
| dy < 0 = 4 + t
package main
import (
"fmt"
"os"
"strings"
)
type State struct {
name string
@willhbr
willhbr / allstrings.go
Last active August 29, 2015 14:16
Go exhaustive language calculator
package main
import (
"fmt"
"math"
)
func size(depth int, alpha *[]string) int {
return int(math.Pow(float64(len(*alpha)), float64(depth)))
}
@willhbr
willhbr / matrix.py
Last active April 1, 2019 12:34
Python Matrix Determinant Calculator
#! py
def solve(matrix, mul):
width = len(matrix)
if width == 1:
return mul * matrix[0][0]
else:
sign = -1
total = 0
for i in range(width):
m = []