Skip to content

Instantly share code, notes, and snippets.

View ttappr's full-sized avatar

Tap ttappr

View GitHub Profile
@ttappr
ttappr / trie_map.rs
Last active January 2, 2024 02:49
A simple map that takes lowercase ascii keys and can hold any sort of value. This can be much faster than a HashMap depending on use case.
//! # TrieMap, A Trie-based Map for Rust
//!
//! Like a HashMap, values are stored and accessible by keys. Lookups can be
//! much faster than a HashMap, but there are some limitations on what can be
//! used for a key, and the memory is potentially higher, but not always.
//!
//! The keys are u8 bytes and the base value and range can be specified. The
//! greater the range, the more memory is used per node. The base value is used
//! to adjust the byte values of the keys to serve as indices into the internal
//! arrays within nodes that hold handles to descendant nodes. The base value
@ttappr
ttappr / ascii_radix.rs
Created December 31, 2023 00:40
A radix sort for vectors of strings with only ASCII compatible characters.
use std::mem::swap;
const ASCII_START: usize = b' ' as usize;
const ASCII_END : usize = b'~' as usize;
fn ascii_count_sort(arr: &mut Vec<String>, place: usize) {
let size = arr.len();
let mut output = vec![String::default(); size];
let mut count = [0; ASCII_END - ASCII_START + 1];
@ttappr
ttappr / snowverload.rs
Last active December 26, 2023 01:25
Advent of Code 2023 Day 25 - Snowverload
//! Advent of Code 2023 Day 25 - Snowverload
//!
//! Part 1: Find the three wires you need to disconnect in order to divide the
//! components into two separate groups. What do you get if you multiply
//! the sizes of these two groups together?
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader};
@ttappr
ttappr / pulse_propagation.rs
Created December 23, 2023 05:14
Advent of Code 2023 Day 20 - Pulse Propagation
//! Advent of Code 2023 Day 20 - Pulse Propagation
//!
//! Part 1: Consult your module configuration; determine the number of low
//! pulses and high pulses that would be sent after pushing the button
//! 1000 times, waiting for all pulses to be fully handled after each
//! push of the button. What do you get if you multiply the total number
//! of low pulses sent by the total number of high pulses sent?
//! Part 2: Reset all modules to their default states. Waiting for all pulses
//! to be fully handled after each button press, what is the fewest
//! number of button presses required to deliver a single low pulse to
@ttappr
ttappr / brent_online.rs
Last active December 24, 2023 00:03
An online (state machine) implementation of the Brent cycle detection algorithm.
//! Brent's Algorithm - Online Version. An instance of `BrentOnline` can be used
//! to find the length of a cycle in a sequence and the start index of the
//! cycle.
//!
//! The object does store a portion of the series internally. So in most cases
//! this module will be suitable; however, if series are analyzed that have
//! very large cycles, some significant memory could be used depending on the
//! value type. Once a cycle is found it immediately drops its stored memory and
//! returns the cycle information.
//!
@ttappr
ttappr / lagoon.rs
Last active December 20, 2023 00:33
Advent of Code 2023 Day 18 - Lavaduct Lagoon
//! Advent of Code 2023 Day 18 - Lavaduct Lagoon
//!
//! Part 1: The Elves are concerned the lagoon won't be large enough; if they
//! follow their dig plan, how many cubic meters of lava could it hold?
//! Part 2: Convert the hexadecimal color codes into the correct instructions;
//! if the Elves follow this new dig plan, how many cubic meters of lava
//! could the lagoon hold?
use std::collections::{HashSet, BTreeSet};
use std::error::Error;
@ttappr
ttappr / crucible.rs
Last active December 17, 2023 15:51
Advent of Code 2023 Day 17 - Clumsy Crucible
//! Advent of Code 2023 Day 17 - Clumsy Crucible
//!
//! Part 1: Direct the crucible from the lava pool to the machine parts factory
//! with the least heat loss.
//! Part 2: Direct the larger crucible from the lava pool to the machine parts
//! factory with the least heat loss.
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::error::Error;
@ttappr
ttappr / camel.rs
Last active December 7, 2023 19:31
Advent of Code 2023 Day 7 - Camel Cards
//! Advent of Code 2023 Day 7 - Camel Cards
//!
//! Part 1: Evaluate and sort the hands, then calculate the winnings.
//! Part 2: Evaluate the hands with Jokers wild, then calculate the winnings.
use std::fs::File;
use std::error::Error;
use std::io::{BufRead, BufReader};
use std::time::Instant;
@ttappr
ttappr / boat_race.rs
Created December 6, 2023 13:27
Advent of Code 2023 Day 6: Wait For It
//! Advent of Code 2023 Day 6 - Wait For It
//!
//! part 1: Calculate the product of the number of t_hold times that win each of
//! the races.
//! Part 2: Given one race with large parameters, calculate the number of t_hold
//! times that win.
use std::fs::read_to_string;
use std::error::Error;
use std::time::Instant;
@ttappr
ttappr / seeds.rs
Created December 5, 2023 11:39
Advent of Code 2023 Day 5 - If You Give A Seed Fertilizer
//! Advent of Code 2023 Day 5 - If You Give A Seed Fertilizer
//!
//! part 1: Find the lowest location corresponding to each seed.
//! Part 2: Find the lowest location corresponding to each seed range.
use std::fs::File;
use std::error::Error;
use std::io::{BufRead, BufReader};
use regex::Regex;