Skip to content

Instantly share code, notes, and snippets.

@unixisevil
unixisevil / fsnn.rs
Created August 11, 2025 10:31
Fairly Simple Neural Network
use std::cmp::Ordering;
use std::num::NonZeroUsize;
use std::{env, error::Error, ffi::OsStr, process};
fn dot_product(xs: &[f32], ys: &[f32]) -> f32 {
xs.iter().zip(ys).map(|(&x, &y)| x * y).sum()
}
fn sigmoid(x: f32) -> f32 {
1.0 / (1.0 + (-x).exp())
#!/usr/bin/env elixir
defmodule KnapSack do
defmodule Item do
defstruct name: "", weight: 0, value: 0.0
def new(name, weight, value), do: %Item{name: name, weight: weight, value: value}
end
def knapsack(items, max_capacity) when is_list(items) and is_integer(max_capacity) do
items_count = length(items)
#!/usr/bin/env elixir
defmodule KMeans do
defprotocol DataPoint do
@type rand_dims :: list(float)
@spec dummy(t, rand_dims) :: t
def dummy(t, rand_dims)
@spec dimensions(t) :: list()
def dimensions(t)
#!/usr/bin/env elixir
defmodule Chromosome do
@type t :: any()
@callback fitness(t) :: Float
@callback random_instance() :: t
@callback crossover(t, t) :: tuple()
@callback mutate(t) :: t
end
#!/usr/bin/env elixir
# Code.require_file("search_utils.exs", ".")
defmodule Edge do
defstruct from: nil, to: nil
def reversed(%Edge{from: f, to: t} = _edge), do: %Edge{from: t, to: f}
defimpl String.Chars do
#!/usr/bin/env elixir
defprotocol Constraint do
@spec variables(t) :: nonempty_list()
def variables(t)
@type assignment() :: map()
@spec satisfied(t, assignment()) :: boolean()
def satisfied(t, assignment)
end
#!/usr/bin/env elixir
defmodule Stack do
def new(), do: []
def push(stack, item) when is_list(stack) do
[item | stack]
end
def pop(stack) when is_list(stack) do
@unixisevil
unixisevil / 2048.rs
Created July 3, 2025 02:38
this bash code ( https://github.com/izabera/bitwise-challenge-2048 ) is a bit hard to decipher, so i ported it for understanding
use crossterm::{
cursor::{Hide, Show},
execute,
terminal::{disable_raw_mode, enable_raw_mode},
};
use std::env;
use std::io::{self, Read, Write, stdin, stdout};
const BG_COLORS: [u8; 12] = [253, 230, 229, 222, 215, 208, 202, 223, 221, 214, 209, 220];
const FG_COLORS: [u8; 12] = [251, 232, 232, 232, 255, 255, 255, 232, 232, 255, 255, 232];
@unixisevil
unixisevil / nucleotide.exs
Created July 1, 2025 13:42
4 bit nucleotide encoding in elixir, ported from this blog https://arianfarid.me/articles/dna-compression.html
#!/usr/bin/env elixir
import Bitwise
defmodule Nucleotide do
defmodule NucWord do
defstruct u16: <<0::16>>
def parse(nucleotides) when is_binary(nucleotides) do
pat = Regex.compile!("^[_ACTGRKYMSWBDHVN]+$")
@unixisevil
unixisevil / ascii_art.exs
Created May 31, 2025 03:02
ascii art in elixir
#!/usr/bin/env elixir
Mix.install([
{:evision, "~> 0.2.13"}
])
alias Evision, as: Cv
alias Evision.Mat
alias Evision.CLAHE