Skip to content

Instantly share code, notes, and snippets.

View cameronp98's full-sized avatar

Cameron Phillips cameronp98

  • Nottingham / Leeds
View GitHub Profile
@cameronp98
cameronp98 / euler_p11.rs
Last active April 25, 2020 10:26
Project Euler problem 11
//! https://projecteuler.net/problem=11
// In the 20×20 grid below, four numbers along a diagonal line have been marked in red.
// 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
// 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
// 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
// 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
// 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
// 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
@cameronp98
cameronp98 / fn_changer.rs
Created March 21, 2020 20:46
Change a function from another task
use tokio::time::{self, Duration};
use tokio::sync::Mutex;
use std::sync::Arc;
type FooFn = dyn Fn(u32) -> u32 + Send;
#[tokio::main]
async fn main() {
let foo: Arc<Mutex<Option<Box<FooFn>>>> = Arc::new(Mutex::new(None));
@cameronp98
cameronp98 / transformations.rs
Last active February 6, 2020 19:34
Identify valid words with one different letter (Lewis Caroll duplet transformations)
use std::io::prelude::*;
use std::io::{self, BufReader};
use std::fs::File;
use std::error::Error;
use std::path::Path;
use std::collections::HashMap;
/// Map describing which words have a character at a given index
/// { letter: { letter_position: Vec<word_id> } }
/// e.g. {'a': {0: [1]}} means "word 1 has an 'a' at position 0"
@cameronp98
cameronp98 / transformations.py
Last active February 5, 2020 01:01
Find valid transformations for Lewis Caroll duplet puzzles, provided a word list 'words.txt'
from collections import defaultdict
words_dict = defaultdict(lambda: defaultdict(list))
# load words
with open('words.txt') as f:
words = [line.rstrip() for line in f]
# build a dict of keys of letter positions, and the letters
# and their parent word: {pos: {letter: [word_index*]}}
@cameronp98
cameronp98 / formatcardinal.rs
Last active September 1, 2019 18:37
Format cardinal numbers in rust
fn cardinal_suffix(x: i64) -> &'static str {
let last_digit = x % 10;
let second_last_digit = x % 100 / 10;
match (second_last_digit, last_digit) {
(1, 1) => "th", // eleventh
(_, 1) => "st", // first
(1, 2) => "th", // twelfth
(_, 2) => "nd", // second
@cameronp98
cameronp98 / querybuilder.rs
Created September 1, 2019 17:50
Filter a collection of elements against a query constructed using the 'builder' pattern
use std::ops::Range;
#[derive(Debug)]
pub struct Person {
name: String,
age: u64,
}
impl Person {
fn new(name: String, age: u64) -> Person {
@cameronp98
cameronp98 / parse_next.rs
Last active July 26, 2019 21:14
Rust trait to parse the next item from `Iterator<Item=&str>`
trait ParseNext {
fn parse_next<T: FromStr>(&mut self) -> T;
}
impl<'a, I: Iterator<Item=&'a str>> ParseNext for I {
fn parse_next<T: FromStr>(&mut self) -> T {
self.next().unwrap().parse().unwrap()
}
}
@cameronp98
cameronp98 / machine.rs
Last active August 2, 2017 23:12
Example state machine with 3 states
use std::sync::mpsc::{channel, Sender, Receiver};
use std::thread::{self, JoinHandle};
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum Event {
Wait,
Work,
Done,
}
@cameronp98
cameronp98 / banana.rs
Created July 28, 2017 23:06
Banana state machine
#[derive(Debug)]
struct StateMachine<S> {
string: String,
state: S,
}
#[derive(Debug)]
struct Start;
impl StateMachine<Start> {
@cameronp98
cameronp98 / main.rs
Created July 28, 2017 22:07
Example worker FSM in rust
mod worker;
use worker::Worker;
fn main() {
let worker = Worker::new()
.initialize()
.do_work()
.reset();
println!("{:?}", worker);