Skip to content

Instantly share code, notes, and snippets.

View robert-king's full-sized avatar

Robert King robert-king

View GitHub Profile
@robert-king
robert-king / builder2.rs
Created October 27, 2025 01:19
builder 2 (no unwrap() needed)
struct Unset;
struct PizzaBuilder<Dough, Topping> {
dough: Dough,
topping: Topping,
}
impl<T> PizzaBuilder<Unset, T> {
fn with_dough(self, dough: &str) -> PizzaBuilder<String, T> {
PizzaBuilder {
@robert-king
robert-king / builder.rs
Created October 25, 2025 04:42
builder pattern
#![feature(type_changing_struct_update)]
use std::marker::PhantomData;
struct Set;
struct Unset;
struct PizzaBuilder<Dough, Topping> {
dough: Option<String>,
topping: Option<String>,
state: PhantomData<(Dough, Topping)>,
@robert-king
robert-king / registry.rs
Created October 6, 2025 19:58
typesafe event registry
use std::marker::PhantomData;
use typemap::{Key, TypeMap};
struct EventListenerKey<E>(PhantomData<E>);
trait EventListner<E>: FnMut(&E) -> () + 'static {}
impl<F, E> EventListner<E> for F where F: FnMut(&E) -> () + 'static {}
@robert-king
robert-king / d2_running_on_fumes.py
Last active October 17, 2024 22:51
meta hacker cup (very hard problem) - discovered linear solution!
"""
Rusty Rob
https://x.com/robertkingNZ
For this hard problem I discovered that an innovative linear solution exists and thought it was worth sharing.
Problem statement
https://www.facebook.com/codingcompetitions/hacker-cup/2020/qualification-round/problems/D2
TLDR Problem statement:
@robert-king
robert-king / meta_hacker_cup_2024_problem_e_wildcard_submissions.rs
Last active October 9, 2024 03:56
meta hacker cup 2024, Problem E: Wildcard Submissions
/*
Problem E: Wildcard Submissions
https://www.facebook.com/codingcompetitions/hacker-cup/2024/round-1/problems/E
I didn't participate but I coded up the solution to E after reading over the contest analysis
*/
use rayon::prelude::*;
use std::io::{stdin, BufRead};
const MODULO: u64 = 998_244_353;
@robert-king
robert-king / linkedlist-iterator.rs
Created September 29, 2024 22:32
rust linkedist with iterators
struct Node {
next: Option<Box<Node>>,
val: i32,
}
impl Node {
fn new(val: i32) -> Self {
Node { next: None, val }
}
@robert-king
robert-king / memo_func.rs
Last active September 8, 2024 20:15
memo function
use std::hash::Hash;
use std::{collections::HashMap, thread, time::Duration};
fn factorial(n: i32) -> i32 {
if n <= 1 {
return 1;
}
thread::sleep(Duration::from_millis(100));
factorial(n - 1).wrapping_mul(n)
}
@robert-king
robert-king / rust-linkedlist.rs
Created September 6, 2024 21:43
proptest linkedlist
#![allow(dead_code)]
/*
we use hashmaps for the linkedlist to sidestep the borrow checker.
we use proptest strategy to generate series of operations across the linkedlist and compare to a VecDeque for correctness.
We can add subtle bugs and proptest manages to catch them
and shrink down to a minimal reproduction which we can then copy and paste to create a new test case.
PROPTEST_CASES=1000000 PROPTEST_MAX_SHRINK_ITERS=100000000 cargo test --release
cargo test copy_test_case
@robert-king
robert-king / cheating_detection.rs
Last active July 26, 2024 08:54
Incredible cheating detection system
/*
- Blazing fast, highly accurate cheating detection algorithm
- by Robert King, https://www.linkedin.com/in/robertkingnz/
- Youtube walkthrough: https://youtu.be/CnIQkIseLGs
- X https://x.com/robertkingNZ
- BACKGROUND
- The Google Code Jam team presented their 90% accurate algorithm after google code jam 2021.
- During the contest, Rob discovered a ~100% accurate algorithm and so thought it was worth sharing.
@robert-king
robert-king / channels.rs
Created May 21, 2024 01:12
Using Rust Channels for Concurrent Programming
// https://youtu.be/zhF-L_BgCHo
use std::sync::{mpsc::channel, Arc, Mutex};
use std::thread;
use std::time::Instant;
fn is_prime(n: i32) -> bool {
if n < 2 {
return false;
}