Skip to content

Instantly share code, notes, and snippets.

View randrews's full-sized avatar

Ross Andrews randrews

View GitHub Profile
@randrews
randrews / primmaze2.rs
Last active November 10, 2024 17:57
Prim's Algorithm + a solver in Rust
use std::collections::VecDeque;
use std::env;
use std::fmt::{Display, Formatter};
use std::ops::{Index, IndexMut};
use rand::RngCore;
use crate::Dir::*;
fn main() {
let args: Vec<_> = env::args().collect();
let dimension = if args.len() < 2 {
@randrews
randrews / primmaze.rs
Created October 23, 2024 04:08
Prim's Algorithm in Rust
use std::env;
use std::fmt::{Display, Formatter};
use std::ops::{Index, IndexMut};
use rand::RngCore; // cargo add rand
use crate::Dir::*;
fn main() {
let args: Vec<_> = env::args().collect();
let dimension = if args.len() < 2 {
println!("No dimension, defaulting to 20");
-- Summing the sequence 1..n in as many ways as I can think of!
-- With the closed form
function sum(n)
return ((n+1) * n) / 2
end
print(sum(6)) -- Had better be 21
-- With tail recursion
@randrews
randrews / telegram.lua
Last active April 18, 2023 04:32
Split a telegram string into component words
function load_dictionary(filename)
local dictionary = {}
for line in io.lines(filename) do
if line:match("^%l%l+$") then
insert(dictionary, line .. "$")
end
end
-- We're cutting all single-letter words, but
@randrews
randrews / bouncy.rs
Created March 27, 2023 04:57
Bouncing circle in Rust
use std::arch::aarch64::float32x4x4_t;
use std::f32::consts::PI;
use std::thread::sleep;
use std::time::Duration;
use pixels::{Pixels, PixelsBuilder};
use pixels::wgpu::TextureFormat;
use raqote::{DrawOptions, DrawTarget, Path, PathBuilder, SolidSource, Source};
use winit::dpi::LogicalSize;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use std::f32::consts::PI;
use pixels::{Pixels, PixelsBuilder};
use pixels::wgpu::TextureFormat;
use raqote::{DrawOptions, DrawTarget, Path, PathBuilder, SolidSource, Source};
use winit::dpi::LogicalSize;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
fn main() -> ! {
@randrews
randrews / lua_map.c
Created April 23, 2011 22:16
Example of embedding Lua in C
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdlib.h>
int map_create(lua_State *lua);
int map_slice(lua_State *lua);
int main(int argc, char **argv){
lua_State *lua = lua_open();
@randrews
randrews / facts.lua
Created April 1, 2011 05:46
Make Lua look like Prolog!
----------------------------------------------------------------------------------------------------
--- Making Lua look like Prolog:
---
--- Let's use metatables for something other than emulating prototype-based OO. By making the
--- __index metamethod create values for undefined things, we can make Lua look like Prolog!
--- We create empty tables for anything starting with a capital letter, functions that populate
--- those tables for lowercase things (to assign relationships) and if a name begins with "is_"
--- then it becomes a function that queries those tables.
----------------------------------------------------------------------------------------------------
@randrews
randrews / hello.lua
Created August 8, 2011 05:04
Embedding Lua in C
-- Pack this into an object file with ld: ld -r -b binary -o hello.o hello.lua
print "Hello, World!"
@randrews
randrews / array.lua
Created July 24, 2011 05:34
Array library for Lua
function string.ordinalize(str)
local num = tonumber(str)
local endings = {"st", "nd", "rd"}
if num >= 11 and num <= 13 then
return num .. "th"
elseif num % 10 > 0 and num % 10 <= #endings then
return num .. endings[num % 10]
else
return num .. "th"
end