Skip to content

Instantly share code, notes, and snippets.

@ylegall
ylegall / challenge.d
Created March 6, 2014 22:18
D challenge template
import std.stdio, std.string, std.conv;
import std.algorithm;
import std.array;
auto readArray(T)()
{
return array(map!(a => to!T(a))(stdin.readln().split()));
}
void main()
@ylegall
ylegall / Euler83.d
Created November 30, 2012 16:05
Shortest Path
import std.array;
import std.algorithm;
import std.conv;
import std.container;
import std.stdio;
import std.string;
import std.typecons;
import std.typetuple;
@ylegall
ylegall / brainfuck.d
Last active December 10, 2015 00:19
brainfuck interpreter
module brainfuck;
import std.algorithm;
import std.conv;
import std.container;
import std.file;
import std.stdio;
enum DATA_SIZE = 1024;
@ylegall
ylegall / smileys.py
Last active December 14, 2015 10:09
Balanced Smileys. (problem from the Facebook Hacker Cup 2013 Hackathon.)
def is_balanced(msg, pos = 0, stack = 0):
for i in range(pos, len(msg)):
c = msg[i]
if c == '(': stack += 1
elif c == ')':
stack -= 1
if stack < 0: return False
elif c == ':':
if i < len(msg)-1:
if msg[i + 1] == '(':
@ylegall
ylegall / getUserDefinedAttributes.d
Last active December 18, 2015 04:19
gist to demonstrate querying methods for annotations (user defined attributes) in D.
import std.traits;
import std.stdio;
// A custom attribute:
struct Test
{
bool enabled = true;
int timeout = 5000;
}
@ylegall
ylegall / randset.d
Last active December 21, 2015 15:19
A data structure with: O(1) random access, O(1) insertion, and O(1) deletion.
/**
* data structure with:
* O(1) random access
* O(1) insertion
* O(1) deletion
* O(1) containment
*/
class RandomAccessSet(T)
{
private {
@ylegall
ylegall / prodcom.d
Created August 26, 2013 23:59
The classic producer–consumer problem (aka the bounded-buffer problem) in D.
import std.stdio;
import core.thread;
import core.sync.condition;
import core.time;
import std.random;
class Buffer(T) {
private {
@ylegall
ylegall / endian.d
Created August 31, 2013 22:54
Determines whether a computer is big-endian or little-endian.
import std.stdio;
auto isLittleEndian() {
int i = 0x11223344;
byte* b = cast(byte*)&i;
return b[0] == 0x44;
}
void main() {
if (isLittleEndian()) writeln("little endian");
@ylegall
ylegall / rotate.d
Created September 3, 2013 23:47
90 degree clockwise matrix rotation.
import std.stdio;
auto rotate(T)(T[][] array) {
foreach(i; 0 .. array.length/2) {
foreach(j; i .. array.length-i-1) {
T temp = array[i][j];
array[i][j] = array[array.length-j-1][i];
array[array.length-j-1][i] = array[array.length-i-1][array.length-j-1];
array[array.length-i-1][array.length-j-1] = array[j][array.length-i-1];
@ylegall
ylegall / minstack.d
Last active December 25, 2015 13:19
A stack with O(1) push, pop, and min
import std.array;
struct MinStack(T)
{
private {
Appender!(T[],T) items;
Appender!(T[],T) mins;
}
auto pop() {