Skip to content

Instantly share code, notes, and snippets.

@madgen
madgen / tetris.pl
Created May 9, 2021 10:09
Solver for tetris puzzles in Prolog
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Transitioning between steps
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
driver(History, History, (_,_,_,FinalBoard), FinalBoard).
driver(History, Trace, State, FinalBoard) :-
% print_state(State),
step(State, NewState),
driver([NewState|History], Trace, NewState, FinalBoard).
% A1 A2 A3 A4
% B1 B2 B3 B4
% C1 C2 C3 C4
% D1 D2 D3 D4
%
% Patient is at D1
% Exit is at A4
%
% Patient is about to be discharged but wants to visit every other patient
% before leaving. Visiting the same patient twice would make him sick.
@madgen
madgen / DatalogUnification.hs
Created October 8, 2019 05:48
Reasonably fast unification for Datalog atoms
#!/usr/bin/env stack
-- stack script --resolver lts-14.7 --package hashable --package hashtables --package vector-sized --package transformers --ghc-options -Wall
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DataKinds #-}
module DatalogUnification (main) where
import Prelude hiding (fail)
import GHC.Generics (Generic)
@madgen
madgen / MinskyGADT.hs
Created June 16, 2018 13:09
How does type inference works on this?
module MinskyGADT where
import Data.Array
import qualified Data.ByteString as BS
import Data.Word (Word8)
data GArrayLike a where
GArray :: Array Int a -> GArrayLike a
GBytes :: BS.ByteString -> GArrayLike Word8
#include <stdio.h>
int main() {
char *cptrs[6] = { "hello", "mellow", "yellow" };
char **cptrs2;
void **vptrs = (void**) cptrs;
vptrs++;
cptrs2 = (char**) vptrs;
printf("%s\n", *cptrs2); // Prints mellow
@madgen
madgen / clean_dispatch.rb
Last active October 10, 2016 21:11
Assign to free variable of a block
def clean_dispatch(&block)
b = block.binding
locals = b.local_variables
params = block.parameters
locals.each do |var|
b.local_variable_set(var, nil) unless params.include? var
end
yield
stg_testMezh ( gcptr smt )
{
W_ itbl;
W_ ttype;
itbl = GET_INFO(UNTAG(smt));
ttype = TO_W_(StgInfoTable_type(itbl));
return (ttype);
}
c Bug 1:
c Mistakenly initialising unspecified common variable.
program nblock
integer x, y
common /name/x, y
call hello
call mello
print *, x, y
end
@madgen
madgen / ruby_to_java_biquine.rb
Created December 20, 2013 02:56
Biquine between Ruby and Java in less than 100 lines of Ruby with use of ASCII table.
# PRELUDE:
# This is my first biquine. It is between Ruby and Java.
# It uses ASCII table to handle issues involving fixed point.
# It should work with all versions of Ruby but it definitely works in 2.0
# It should work with all versions of Java and definitely works with 7.
# It is not super DRY so it can be refactored to be shorter but 96 lines
# is not so bad I guess (I know it is pretty terrible).
# Also it is fun to write so if you haven't tried, I highly reccommend it.
@madgen
madgen / One line substitution cipher decipherer
Last active December 20, 2015 00:59
So bored that just wrote a oneliner for deciphering (or more like brute forcing) substitution cipher.
# Works on English alphabet with capital letters.
# Code with example usage
ruby -e "(1..25).each {|i| puts ARGV[0].chars.map {|e| Hash[('A'..'Z').zip(0..25)].invert[(Hash[('A'..'Z').zip(0..25)][e] + i) % 26]}.join('') + %Q|\n\n|}" ESLRPPAKPPFAUWPPKGPPOWPPSJWPPFAUW
# Output
FTMSQQBLQQGBVXQQLHQQPXQQTKXQQGBVX
GUNTRRCMRRHCWYRRMIRRQYRRULYRRHCWY