Skip to content

Instantly share code, notes, and snippets.

View dcoles's full-sized avatar

David Coles dcoles

View GitHub Profile
@dcoles
dcoles / fsm.rs
Last active October 12, 2021 04:33
Dynamic Finite-State-Machine in Rust
use std::fmt::Debug;
pub trait State: Debug {
fn execute(&self) -> Transition;
}
pub trait TransitionTo<S: State + 'static>: State {
fn transition_to(&self, to: &'static S) -> Transition {
Transition { to }
}
# Print the number of bytes unread in a fifo.
# David Coles <coles.david@gmail.com>
import argparse
import ctypes
import fcntl
import os
import sys
import termios
@dcoles
dcoles / alarmtimer.py
Created March 20, 2012 07:30
POSIX Alarm Timers example using Python ctypes
#!/usr/bin/env python
# POSIX Alarm Timer example using Python ctypes
#
# Author: David Coles <coles.david@gmail.com>
# Date: 2012-03-19
#
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.
@dcoles
dcoles / draft.rs
Created December 1, 2021 19:51
Advent of Code 2021: Day 1 (Draft vs. Final)
fn main() {
let input = read_input_from_file("day01/input.txt").unwrap();
// Part 1
let mut count = 0;
let mut last = -1;
for &n in &input {
if n > last {
count += 1;
}
@dcoles
dcoles / 452874.py
Last active December 20, 2021 21:53
By only adding + - * into the string 452874 (in that order), how many way can you get to -18?
"""
By only adding + - * into the string 452874 (in that order),
how many way can you get to -18?
"""
N = "452874"
M = -18
OPERATIONS = [None, '+', '-', '*']
@dcoles
dcoles / inline.py
Created November 14, 2012 09:13
Inline assembly in Python
import ctypes
import sys
import os
import errno
FUNC = ctypes.CFUNCTYPE(None)
PROT_NONE = 0
PROT_READ = 1
PROT_WRITE = 2