Skip to content

Instantly share code, notes, and snippets.

@MrTrick
MrTrick / gist:1011791
Created June 7, 2011 06:51
Mark Miller's sealer / unsealer code
function makeBrand() {
var key = {};
class Envelope(payload) {
protected const contents = payload;
function open(k) {
if (k === key) { return this.contents; }
}
}
return {
sealer: function(payload) { return new Envelope(payload); },
@MrTrick
MrTrick / gist:1064300
Created July 5, 2011 05:40
17x17 max solution
+V----------------+
| # # # <
| # # # # # ######|
| # # # # # |
| # # # # ########|
| # # # # # |
| # # # # # ######|
| # # # |
|######## ####### |
| |
<?
set_time_limit(0);
function usage($msg) {
echo $msg."\n";
echo "Usage: update_couch_design ADMIN_PASSWORD\n";
echo "Updates the configured couchdb database's design doc and views without blocking any other users.\n";
exit;
}
//Must run as a standalone script.
@MrTrick
MrTrick / *with* optimisation.c
Created July 19, 2011 03:48
AVRGCC Generated ASM code
if (PORTB&(1<<USER_SOL_PIN)) {
48e: c0 9b sbis 0x18, 0 ; 24
490: 02 c0 rjmp .+4 ; 0x496 <main+0x12>
PORTD|=(1<<UV_LAMP_PIN);
492: 97 9a sbi 0x12, 7 ; 18
PORTB&=~(1<<BW_PUMP_PIN);
494: c6 98 cbi 0x18, 6 ; 24
496: ff cf rjmp .-2 ; 0x496 <main+0x12>
Same code generated with macros:
<?
//Given MSA files, report for each the semesters.
array_shift($argv);
if (!$argv) $argv = array('php://stdin');
foreach ($argv as $file) {
$in = new SplFileObject($file, 'r');
$semesters = array();
foreach($in as $l)
if (preg_match('/\d{5}_([^_]+)_.*\|(\d{4})$/',$l,$m))
<?
...
$select = new Zend_Db_Select($db=Zend_Db_Table::getDefaultAdapter());
$select->from(array('a'=>'tutors_applicant_application'));
//Filter on area?
if (isset($query['area']) and $query['area']!='all') {
$area = $query['area'];
$select->where('area=?',$area);
}
@MrTrick
MrTrick / gist:1498931
Created December 19, 2011 21:21
Parallel Troll Circuit
http://www.youtube.com/watch?feature=player_embedded&v=OoPCDiMF108
1. In the battery or inside the battery plug, the current is converted to AC.
2. The LEDs are attached in opposite polarity, but are normal LEDs.
3. The switches are labelled in reverse - '0' is short, '1' is open.
4. Each switch has an internal diode in series, with the same polarity as their LED.
When both switches are '0':
Each switch conducts when the AC cycle forward-biases its diode, so the LED sees insufficient voltage (only 0.7V to light)
@MrTrick
MrTrick / gist:1535708
Created December 29, 2011 19:13
DAWG collapsing
@Test
public void testDawgDictionaryCollapse() {
Dictionary dictionary = Dictionary.buildDefaultDictionary();
Trie trie = new Trie(dictionary);
Dawg dawg = new Dawg(dictionary);
assertEquals(395184, trie.countNodes());
int i=0;
//Despite the merged nodes, a dawg should look structurally the same as a trie.
TrieIterator t = trie.iterator(), d = dawg.iterator();
@MrTrick
MrTrick / gist:1536892
Created December 30, 2011 00:23
Terrible hashing function
public class Node {
public final static boolean GADDAG_PREPEND = false, GADDAG_APPEND = true;
Node[] children = new Node[CHILD_COUNT];
boolean end_of_word = false, gaddag_mode = GADDAG_PREPEND;
char letter;
...
/**
@MrTrick
MrTrick / gist:1544408
Created December 31, 2011 15:56
Beautiful code
public ScrabbleGame() {
super(
ScrabbleGame.parseGrid(
"T..d...T...d..T\n"+
".D...t...t...D.\n"+
"..D...d.d...D..\n"+
"d..D...d...D..d\n"+
"....D.....D....\n"+
".t...t...t...t.\n"+
"..d...d.d...d..\n"+