Skip to content

Instantly share code, notes, and snippets.

View anthonyclays's full-sized avatar

Anthony Clays anthonyclays

  • Leuven, Belgium
View GitHub Profile
@anthonyclays
anthonyclays / perfect_squares.rs
Created September 14, 2015 08:37
Fast perfect square test in Rust
fn isqrt(n: usize) -> usize {
n == 0 && return n;
let mut s = (n as f64).sqrt() as usize;
s = (s + n / s) >> 1;
if s * s > n { s - 1 } else { s }
}
fn perfect_sqrt(n: usize) -> isize {
match n & 0xf {
0 | 1 | 4 | 9 => {
@anthonyclays
anthonyclays / meta_fizzbuzz.py
Created March 28, 2016 15:34
meta-FizzBuzz
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from itertools import combinations
from operator import mul
def all_combinations(things):
for i in xrange(len(things), 0, -1):
for result in combinations(things, i):
yield result
@anthonyclays
anthonyclays / server.py
Created April 12, 2019 15:03
Simple server that runs a process for each client connection
#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
import asyncio
import click
async def copy(src, dst):
buf = await src.read(1024)
while buf:
dst.write(buf)
@anthonyclays
anthonyclays / minesweeper.py
Created April 18, 2017 11:23
Guesswork-free 3D minesweeper
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from itertools import product, chain, count
import random, time
from z3 import *
N, M = 6, 11
@anthonyclays
anthonyclays / keybase.md
Created April 12, 2017 21:11
keybase.md

Keybase proof

I hereby claim:

  • I am anthonyclays on github.
  • I am anthonyclays (https://keybase.io/anthonyclays) on keybase.
  • I have a public key ASCkuz87qTzF5PVYgrNV8vypyCjhDFDm2MJwOWe6OpdBIQo

To claim this, I am signing this object:

@anthonyclays
anthonyclays / reveng_of_the_pancakes.pl
Last active April 10, 2016 08:38
Revenge of the pancakes
<>; # ignore first line of input
foreach(<>) {
chomp; # get rid of the newline
$t++; # increment case number (implicitly initialized to zero)
s/([+-])\1+/\1/g; # simplify stack of pancakes: consecutive similarly-facing pancakes are squashed together
s/\+$//; # if the last pancake is already facing the correct way: ignore it
$l=length; # get the height of the stack
print"Case #$t: $l\n" # print it
}
@anthonyclays
anthonyclays / partition.jl
Created September 11, 2015 15:23
Vector partitioning into n chunks
chunks(l::AbstractVector, n::Int) = let part_l = length(l) / n
[let start = floor(Int, (i-1)*part_l + 1), stop = floor(Int, i*part_l);
l[start:stop] end for i in 1:n]
end
@anthonyclays
anthonyclays / sr830_repl.py
Created September 11, 2015 13:27
SR830 repl.
#!/usr/bin/env python
# encoding: utf-8
"""
Usage:
rlwrap ./srs_test.py
"""
import os
@anthonyclays
anthonyclays / generated_llvm
Last active August 29, 2015 14:27
LLVM wizardry
julia> code_llvm(g, (Int,))
define i64 @julia_g_20191(i64) {
top:
%1 = icmp sgt i64 %0, 0, !dbg !8
br i1 %1, label %L.preheader, label %L3, !dbg !8
L.preheader: ; preds = %top
%2 = add i64 %0, -1, !dbg !8
%3 = zext i64 %2 to i65
@anthonyclays
anthonyclays / typesort.jl
Last active August 29, 2015 14:22
Typelevel quicksort (and integer arithmetic) in Julia
#!/usr/bin/env julia
# Peano arithmetic (https://en.wikipedia.org/wiki/Peano_axioms)
abstract Natural <: Integer
immutable Zero <: Natural end
immutable Successor{P<:Natural} <: Natural end
# Define a total order relation
<(::Type{Zero}, ::Type{Zero}) = false
<{P<:Natural}(::Type{Zero}, ::Type{Successor{P}}) = true