Skip to content

Instantly share code, notes, and snippets.

@mharju
mharju / outrun.clj
Last active Aug 29, 2015
Solution to the newest Reaktor FastTrack. Mathematically elegant, code-wise maybe not so :)
View outrun.clj
; 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 Jan 13, 2014
Ecom solution
View ecom.js
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 Jan 1, 2016
Reaktor FastTrack morse code DFA solution
View build-dfa.clj
(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 Nov 8, 2013
Reactive Cocoa. Don't you just got to love it?
View gist:7367279
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 Dec 22, 2015
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).
View reaktor-max.js
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 Dec 18, 2015
Encoding and decoding QWERTY-ciphers with constant transposition of one letter.
View qwerty-cipher.py
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 Dec 17, 2015
Fixed the glitch.
View trivial-opengl-blit
// 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 Dec 14, 2015
Not so buggy filter code
View filter.c
// Ah. Better.
typedef struct {
float x1, x2, y1, y2;
float c0, c1, c2, c3, c4;
} biquad_t;
typedef enum {
HIGH_SHELF,
LOW_SHELF,
View google-analytics-trackevent.js
// 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 Dec 11, 2015
what the....
View gist:4538988
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;
}