porting over AOC from previous years to a monorepo.
This commit is contained in:
commit
84c4cf9991
194 changed files with 30104 additions and 0 deletions
6
2023/aoc_libs/Cargo.toml
Normal file
6
2023/aoc_libs/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "aoc_libs"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
description.workspace = true
|
||||
3
2023/aoc_libs/src/distances.rs
Normal file
3
2023/aoc_libs/src/distances.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub trait Distances{
|
||||
fn taxicab_distance(&self, other: &Self)->usize;
|
||||
}
|
||||
4
2023/aoc_libs/src/lib.rs
Normal file
4
2023/aoc_libs/src/lib.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub mod points;
|
||||
pub mod range;
|
||||
pub mod misc;
|
||||
pub mod distances;
|
||||
43
2023/aoc_libs/src/misc.rs
Normal file
43
2023/aoc_libs/src/misc.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
pub fn arr_lcm(input: &[usize]) -> usize {
|
||||
input.iter().copied().reduce(lcm).unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn lcm(first: usize, second: usize) -> usize {
|
||||
first * second / gcd(&first, &second)
|
||||
}
|
||||
|
||||
pub fn gcd(a: &usize, b: &usize) -> usize {
|
||||
let mut a = *a;
|
||||
let mut b = *b;
|
||||
while b != 0 {
|
||||
let tmp = b;
|
||||
b = a.rem_euclid(b);
|
||||
a = tmp;
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_arr_lcm() {
|
||||
assert_eq!(arr_lcm(&[4, 6]), 12);
|
||||
assert_eq!(arr_lcm(&[5, 2]), 10);
|
||||
assert_eq!(arr_lcm(&[5, 2, 6]), 30);
|
||||
assert_eq!(arr_lcm(&[5, 2, 6, 3]), 30);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lcm() {
|
||||
assert_eq!(lcm(4, 6), 12);
|
||||
assert_eq!(lcm(5, 2), 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gcd() {
|
||||
assert_eq!(gcd(&8, &12), 4);
|
||||
assert_eq!(gcd(&54, &24), 6);
|
||||
}
|
||||
}
|
||||
229
2023/aoc_libs/src/points.rs
Normal file
229
2023/aoc_libs/src/points.rs
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
use crate::distances::Distances;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
|
||||
pub struct Point {
|
||||
pub x: isize,
|
||||
pub y: isize,
|
||||
}
|
||||
|
||||
impl Point {
|
||||
/// converts a point (representing a point on a 4 quadrant grid with positive xy in the
|
||||
/// top-right) into a upoint (representing a point on a 1 quadrant grid with the origin in the
|
||||
/// top-left corner). Returns none if the resulting point would have either number negative.
|
||||
pub fn to_upoint(self, zero_coord: &UPoint) -> Option<UPoint> {
|
||||
Some(UPoint {
|
||||
x: zero_coord.x.checked_add_signed(self.x)?,
|
||||
y: zero_coord.y.checked_add_signed(-self.y)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Distances for Point {
|
||||
fn taxicab_distance(&self, other: &Self) -> usize {
|
||||
self.x.abs_diff(other.x) + self.y.abs_diff(other.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add for Point {
|
||||
type Output = Point;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
x: self.x + rhs.x,
|
||||
y: self.y + rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::AddAssign for Point {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
*self = *self + rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub for Point {
|
||||
type Output = Point;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::SubAssign for Point {
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
*self = *self - rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Neg for Point {
|
||||
type Output = Point;
|
||||
|
||||
fn neg(self) -> Self::Output {
|
||||
Point {
|
||||
x: -self.x,
|
||||
y: -self.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// an unsigned point in 2d space
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct UPoint {
|
||||
pub x: usize,
|
||||
pub y: usize,
|
||||
}
|
||||
|
||||
impl UPoint {
|
||||
/// converts a upoint (representing a point on a 1 quadrant grid with the origin in the
|
||||
/// top-left corner) into a point( representing a point on a 4 quadrant grid with positive xy
|
||||
/// in the top-right)
|
||||
pub fn to_point(self, zero_coord: &UPoint) -> Point {
|
||||
Point {
|
||||
x: -(zero_coord.x as isize - self.x as isize),
|
||||
y: zero_coord.y as isize - self.y as isize,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Distances for UPoint {
|
||||
fn taxicab_distance(&self, other: &Self) -> usize {
|
||||
self.x.abs_diff(other.x) + self.y.abs_diff(other.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add for UPoint {
|
||||
type Output = UPoint;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
x: self.x + rhs.x,
|
||||
y: self.y + rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::AddAssign for UPoint {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
*self = *self + rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub for UPoint {
|
||||
type Output = UPoint;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::SubAssign for UPoint {
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
*self = *self - rhs
|
||||
}
|
||||
}
|
||||
|
||||
/// A matrix that allows negative co-oordinates. Will panic if referencing out of bounds, just like
|
||||
/// a normal 2d array.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct FourQuadrantMatrix<const X: usize, const Y: usize, T> {
|
||||
matrix: [[T; X]; Y],
|
||||
max_point: Point,
|
||||
min_point: Point,
|
||||
zero_coord: UPoint,
|
||||
}
|
||||
|
||||
impl<const X: usize, const Y: usize, T> FourQuadrantMatrix<{ X }, { Y }, T>
|
||||
where
|
||||
T: Copy,
|
||||
T: Default,
|
||||
{
|
||||
/// generates a new FourQuadrantMatrix with a given zero point (the point in the underlying 2d
|
||||
/// array considered to be (0,0))
|
||||
pub fn new(zero_coord: UPoint) -> FourQuadrantMatrix<{ X }, { Y }, T> {
|
||||
FourQuadrantMatrix {
|
||||
matrix: [[T::default(); X]; Y],
|
||||
max_point: UPoint { x: X - 1, y: 0 }.to_point(&zero_coord),
|
||||
min_point: UPoint { x: 0, y: Y - 1 }.to_point(&zero_coord),
|
||||
zero_coord,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn zero_coord(&self) -> UPoint {
|
||||
self.zero_coord
|
||||
}
|
||||
|
||||
pub fn min_point(&self) -> Point {
|
||||
self.min_point
|
||||
}
|
||||
|
||||
pub fn max_point(&self) -> Point {
|
||||
self.max_point
|
||||
}
|
||||
|
||||
/// makes sure a point is in bounds and if not, brings it in bounds.
|
||||
pub fn bound_point(&self, point: &mut Point) {
|
||||
if point.x > self.max_point.x {
|
||||
point.x = self.max_point.x
|
||||
}
|
||||
|
||||
if point.y > self.max_point.y {
|
||||
point.y = self.max_point.y
|
||||
}
|
||||
|
||||
if point.x < self.min_point.x {
|
||||
point.x = self.min_point.x
|
||||
}
|
||||
|
||||
if point.y < self.min_point.y {
|
||||
point.y = self.min_point.y
|
||||
}
|
||||
}
|
||||
|
||||
/// checks if the point is in bounds.
|
||||
pub fn is_in_bounds(&self, point: &Point) -> bool {
|
||||
point.x <= self.max_point.x
|
||||
&& point.y <= self.max_point.y
|
||||
&& point.x >= self.min_point.x
|
||||
&& point.y >= self.min_point.y
|
||||
}
|
||||
/// fills the matrix with the Ts default value.
|
||||
pub fn reset_matrix(&mut self) {
|
||||
self.matrix = [[T::default(); X]; Y];
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const X: usize, const Y: usize> std::ops::IndexMut<Point>
|
||||
for FourQuadrantMatrix<{ X }, { Y }, T>
|
||||
{
|
||||
fn index_mut(&mut self, index: Point) -> &mut Self::Output {
|
||||
let upoint = index
|
||||
.to_upoint(&self.zero_coord)
|
||||
.expect("would result in negative unsigned coordinate!");
|
||||
&mut self.matrix[upoint.y][upoint.x]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const X: usize, const Y: usize> std::ops::Index<Point>
|
||||
for FourQuadrantMatrix<{ X }, { Y }, T>
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, index: Point) -> &Self::Output {
|
||||
let upoint = index
|
||||
.to_upoint(&self.zero_coord)
|
||||
.expect("would result in negative unsigned coordinate!");
|
||||
&self.matrix[upoint.y][upoint.x]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const X: usize, const Y: usize> From<FourQuadrantMatrix<{ X }, { Y }, T>> for [[T; X]; Y] {
|
||||
fn from(value: FourQuadrantMatrix<{ X }, { Y }, T>) -> Self {
|
||||
value.matrix
|
||||
}
|
||||
}
|
||||
118
2023/aoc_libs/src/range.rs
Normal file
118
2023/aoc_libs/src/range.rs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
// #[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
// /// range that includes the start, but excludes the end.
|
||||
// pub struct Range<T>
|
||||
// where
|
||||
// T: PartialOrd + Ord + PartialEq + Copy,
|
||||
// {
|
||||
// start: T,
|
||||
// end: T,
|
||||
// }
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
pub trait RangeIntersection {
|
||||
fn any_overlap(&self, other: &Self) -> bool;
|
||||
fn calc_intersection(&self, other: &Self) -> Option<Self>
|
||||
where
|
||||
Self: std::marker::Sized;
|
||||
fn complete_overlap(&self, other: &Self) -> bool;
|
||||
}
|
||||
|
||||
impl<T> RangeIntersection for Range<T>
|
||||
where
|
||||
T: PartialOrd + Ord + PartialEq + Copy,
|
||||
{
|
||||
/// calcs whether self and other overlap at all. symettric.
|
||||
fn any_overlap(&self, other: &Self) -> bool {
|
||||
if self.is_empty() || other.is_empty() {
|
||||
false
|
||||
} else {
|
||||
self.start < other.end && self.end > other.start
|
||||
}
|
||||
}
|
||||
|
||||
/// calculates the range that is part of both ranges.
|
||||
/// Returns None if the ranges do not overlap.
|
||||
fn calc_intersection(&self, other: &Self) -> Option<Self> {
|
||||
if self.any_overlap(other) {
|
||||
Some(self.start.max(other.start)..self.end.min(other.end))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///calcs whether self completely contains other
|
||||
fn complete_overlap(&self, other: &Self) -> bool {
|
||||
if self.is_empty() || other.is_empty() {
|
||||
false
|
||||
} else {
|
||||
self.start <= other.start && self.end >= other.end
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn calc_intersection() {
|
||||
let a = 1..5;
|
||||
let b = 3..9;
|
||||
let c = 4..6;
|
||||
let d = 6..8;
|
||||
let e = 1..4;
|
||||
assert_eq!(a.calc_intersection(&b), Some(3..5));
|
||||
assert_eq!(b.calc_intersection(&a), Some(3..5));
|
||||
|
||||
assert_eq!(a.calc_intersection(&d), None);
|
||||
assert_eq!(d.calc_intersection(&a), None);
|
||||
|
||||
assert_eq!(c.calc_intersection(&b), Some(c.clone()));
|
||||
assert_eq!(b.calc_intersection(&c), Some(c.clone()));
|
||||
|
||||
assert_eq!(e.calc_intersection(&b), Some(3..4));
|
||||
assert_eq!(b.calc_intersection(&e), Some(3..4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_any_overlap() {
|
||||
let a = 1..5;
|
||||
let b = 3..9;
|
||||
let c = 4..6;
|
||||
let d = 6..8;
|
||||
let e = 1..4;
|
||||
assert!(a.any_overlap(&b));
|
||||
assert!(b.any_overlap(&a));
|
||||
|
||||
assert!(b.any_overlap(&c));
|
||||
assert!(c.any_overlap(&b));
|
||||
|
||||
assert!(!d.any_overlap(&a));
|
||||
assert!(!a.any_overlap(&d));
|
||||
|
||||
assert!(e.any_overlap(&b));
|
||||
assert!(b.any_overlap(&e));
|
||||
|
||||
assert!(!e.any_overlap(&d));
|
||||
assert!(!d.any_overlap(&e));
|
||||
}
|
||||
#[test]
|
||||
fn test_complete_overlap() {
|
||||
let a = 1..5;
|
||||
let b = 3..9;
|
||||
let c = 4..6;
|
||||
let d = 6..8;
|
||||
let e = 1..4;
|
||||
assert!(a.complete_overlap(&a));
|
||||
|
||||
assert!(a.complete_overlap(&e));
|
||||
assert!(!e.complete_overlap(&a));
|
||||
|
||||
assert!(b.complete_overlap(&c));
|
||||
assert!(!c.complete_overlap(&b));
|
||||
|
||||
assert!(b.complete_overlap(&d));
|
||||
assert!(!d.complete_overlap(&b));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue