Skip to content

Instantly share code, notes, and snippets.

@mharju
mharju / outrun.clj
Last active August 29, 2015 13:57
Solution to the newest Reaktor FastTrack. Mathematically elegant, code-wise maybe not so :)
; A solution to the latest http://reaktor.fi/careers/fast_track/
;
; Calculates recursively the maximum path function
; S(i,j) = W(i,j) + max { S(i-1, j-1), S(i-1, j) }
;
; Solution to the problem is just finding
; M = max { S(N, i) }, 1 <= i <= N (using 1-based indexing)
;
; then prints out the solution from the given node as an HTML table.
@mharju
mharju / ecom.js
Created January 13, 2014 20:04
Ecom solution
function calculate(o){return _.foldl(o,function(a,b){a[b.vat]=b.price+(a[b.vat]||0);return a},{})}
@mharju
mharju / build-dfa.clj
Last active January 1, 2016 10:39
Reaktor FastTrack morse code DFA solution
(ns test.core
(:require [clojure.string :as str]))
(def morse
(into []
(sort-by key (comparator (fn [x y] (<= (count x) (count y))))
{".-" "A" "-..." "B" "-.-." "C" "-.." "D" "." "E" "..-." "F" "--." "G" "...." "H" ".." "I" ".---" "J" "-.-" "K" ".-.." "L" "--" "M" "-." "N" "---" "O" ".--." "P" "--.-" "Q" ".-." "R" "..." "S" "-" "T" "..-" "U" "...-" "V" ".--" "W" "-..-" "X" "-.--" "Y" "--.." "Z"})))
(defn build-dfa
([codes] (build-dfa codes []))
@mharju
mharju / gist:7367279
Created November 8, 2013 07:01
Reactive Cocoa. Don't you just got to love it?
NSArray *items = @[_gainSlider, _frequencySaturation, _driveSlider];
NSArray *signals = [[[items rac_sequence] map:^id(id value) {
return [value rac_highlightSignal];
}] array];
__block BOOL triggering = NO;
[[RACSignal merge:signals]
subscribeNext:^(RACTuple* value) {
if(triggering) { return; }
triggering = YES;
@mharju
mharju / reaktor-max.js
Last active December 22, 2015 07:38
Splits the array to parts of equal lengths, len = 3,4,5,...,35 and searches that space for numbers N satisfying N=x(x+1)(x+2)(x+3)(x+4).
var find_greatest = function(input) {
var check = function(n) {
if(n==0) { return false; }
// n is of form x^5 + O(x^4), so we can pick it by taking the
// fifth root and taking the extra crust off
var x = Math.floor( Math.pow( n, 0.2 ) ) - 1
return n === x*(x+1)*(x+2)*(x+3)*(x+4);
}
var slice_n = function(n,x) {
@mharju
mharju / qwerty-cipher.py
Last active December 18, 2015 07:29
Encoding and decoding QWERTY-ciphers with constant transposition of one letter.
qwerty = u"qwertyuiopåasdfghjklöä'zxcvbnm,"
transpose = lambda s,t: ''.join([ (qwerty[qwerty.index(c)+t] if c in qwerty else c) for c in s])
encode = lambda s: transpose(s, 1)
decode = lambda s: transpose(s, -1);
@mharju
mharju / trivial-opengl-blit
Last active December 17, 2015 10:49
Fixed the glitch.
// Texture: 1024 x 1024 x 4bpp
// Contains 1024 x 768 image from (0,0) to (1024, 768)
// Use glOrtho like glOrtho2d
glOrtho(0, width, height, 0, -1, 1);
// This solved it!
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 1.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
@mharju
mharju / filter.c
Last active December 14, 2015 22:38
Not so buggy filter code
// Ah. Better.
typedef struct {
float x1, x2, y1, y2;
float c0, c1, c2, c3, c4;
} biquad_t;
typedef enum {
HIGH_SHELF,
LOW_SHELF,
@mharju
mharju / google-analytics-trackevent.js
Created March 5, 2013 06:58
Why does this not work?
// init:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-FOOBAR-X']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
@mharju
mharju / gist:4538988
Last active December 11, 2015 03:38
what the....
double direction_x = 0.0, direction_y = 0.0, direction = 0.0;
CGPoint d = CGPointMake(current.x - previous.x, current.y - previous.y);
if(d.x != 0) {
if(current.y <= kCenterY) {
if(d.x > 0.0) {
direction_x = 1.0;
} else {
direction_x = -1.0;
}