From 50f451efd2a0ad5cedbf68fbe014201adcf658fd Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sat, 14 Dec 2024 11:42:26 +0100 Subject: [PATCH] added inclimple day 12 --- examples/2024/12.txt | 1 + examples/2024/12a.txt | 4 + examples/2024/12b.txt | 5 ++ examples/2024/12c.txt | 10 +++ examples/2024/12d.txt | 5 ++ examples/2024/12f.txt | 6 ++ macros/src/lib.rs | 36 +++------ src/coordinate_systems/euclidian.rs | 3 + src/coordinate_systems/kartesian.rs | 119 ++++++++++++++++------------ src/lib.rs | 28 ++++++- src/strings.rs | 59 ++++++-------- 11 files changed, 162 insertions(+), 114 deletions(-) create mode 120000 examples/2024/12.txt create mode 100644 examples/2024/12a.txt create mode 100644 examples/2024/12b.txt create mode 100644 examples/2024/12c.txt create mode 100644 examples/2024/12d.txt create mode 100644 examples/2024/12f.txt diff --git a/examples/2024/12.txt b/examples/2024/12.txt new file mode 120000 index 0000000..f019785 --- /dev/null +++ b/examples/2024/12.txt @@ -0,0 +1 @@ +12c.txt \ No newline at end of file diff --git a/examples/2024/12a.txt b/examples/2024/12a.txt new file mode 100644 index 0000000..b41163a --- /dev/null +++ b/examples/2024/12a.txt @@ -0,0 +1,4 @@ +AAAA +BBCD +BBCC +EEEC diff --git a/examples/2024/12b.txt b/examples/2024/12b.txt new file mode 100644 index 0000000..50a7304 --- /dev/null +++ b/examples/2024/12b.txt @@ -0,0 +1,5 @@ +OOOOO +OXOXO +OOOOO +OXOXO +OOOOO diff --git a/examples/2024/12c.txt b/examples/2024/12c.txt new file mode 100644 index 0000000..85b768f --- /dev/null +++ b/examples/2024/12c.txt @@ -0,0 +1,10 @@ +RRRRIICCFF +RRRRIICCCF +VVRRRCCFFF +VVRCCCJFFF +VVVVCJJCFE +VVIVCCJJEE +VVIIICJJEE +MIIIIIJJEE +MIIISIJEEE +MMMISSJEEE diff --git a/examples/2024/12d.txt b/examples/2024/12d.txt new file mode 100644 index 0000000..5818d30 --- /dev/null +++ b/examples/2024/12d.txt @@ -0,0 +1,5 @@ +EEEEE +EXXXX +EEEEE +EXXXX +EEEEE diff --git a/examples/2024/12f.txt b/examples/2024/12f.txt new file mode 100644 index 0000000..f7cd38f --- /dev/null +++ b/examples/2024/12f.txt @@ -0,0 +1,6 @@ +AAAAAA +AAABBA +AAABBA +ABBAAA +ABBAAA +AAAAAA diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 2cd4cf6..198abca 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -22,11 +22,7 @@ struct IncludeData { impl Default for IncludeData { fn default() -> Self { - IncludeData { - var: VAR_NAME.into(), - year: None, - day: None, - } + IncludeData { var: VAR_NAME.into(), year: None, day: None } } } @@ -37,14 +33,8 @@ fn get_text(item: T) -> Result { None => { let start = span.start(); let end = span.end(); - Err(Error::new( - span, - format!( - "Failed to get sourcetext for {}:{}-{}:{}", - start.line, start.column, end.line, end.column, - ), - )) - } + Err(Error::new(span, format!("Failed to get sourcetext for {}:{}-{}:{}", start.line, start.column, end.line, end.column,))) + }, } } @@ -73,7 +63,7 @@ fn canonicalize(dir: &str, input: IncludeData) -> Option { } else { None } - } + }, Err(_) => None, } } @@ -113,15 +103,12 @@ pub fn include_data(data: TokenStream) -> TokenStream { #[doc = #comment] const #ident: &str = include_str!(#p).trim_ascii(); } - .into() - } + .into(); + }, None => comment = "failed to get data from the paths".into(), } } else { - comment = format!( - "Failed to get the year({:?}) or day({:?}) falling back to default", - input.year, input.day - ); + comment = format!("Failed to get the year({:?}) or day({:?}) falling back to default", input.year, input.day); } quote! { #[doc = #comment] @@ -148,15 +135,12 @@ pub fn include_example(data: TokenStream) -> TokenStream { #[doc = #comment] const #ident: &str = include_str!(#p).trim_ascii(); } - .into() - } + .into(); + }, None => comment = "failed to get data from the path".into(), } } else { - comment = format!( - "Failed to get the year({:?}) or day({:?}) falling back to default", - input.year, input.day - ); + comment = format!("Failed to get the year({:?}) or day({:?}) falling back to default", input.year, input.day); } quote! { #[doc = #comment] diff --git a/src/coordinate_systems/euclidian.rs b/src/coordinate_systems/euclidian.rs index 8b9911d..375ab08 100644 --- a/src/coordinate_systems/euclidian.rs +++ b/src/coordinate_systems/euclidian.rs @@ -60,4 +60,7 @@ impl Euclidian { #[cfg(test)] mod test { use super::*; + fn test() { + Euclidian { x: 0, y: 0, z: 0 }; + } } diff --git a/src/coordinate_systems/kartesian.rs b/src/coordinate_systems/kartesian.rs index 2d797e2..8d6fa25 100644 --- a/src/coordinate_systems/kartesian.rs +++ b/src/coordinate_systems/kartesian.rs @@ -1,5 +1,11 @@ +use log::debug; use num::*; -use std::{fmt::Display, ops::*}; +use std::{ + fmt::{Debug, Display}, + ops::*, +}; + +pub type KD = KartesianDirection; pub trait MaximumFromMap { fn maximum(map: &Vec>) -> Kartesian; @@ -83,7 +89,19 @@ impl Kartesian { } } -impl Kartesian { +impl Kartesian { + pub fn get_value(self, map: &Vec>) -> Option { + if map.len() > self.x { + if map[self.x].len() > self.y { + return Some(map[self.x][self.y]); + } + } + + None + } +} + +impl Kartesian { pub fn checked_add_max(self, rhs: Kartesian, max: Kartesian) -> Option> { let mut new = Kartesian::default(); new.x = match self.x.checked_add(&rhs.x) { @@ -120,35 +138,25 @@ impl Kartesian { } pub fn diff(self, rhs: Kartesian) -> (Kartesian, KartesianDirection) { - let mut k = Kartesian::::default(); + let mut relative = Kartesian::::default(); let mut dir = KartesianDirection::None; - k.x = match (self.x.checked_sub(&rhs.x), rhs.x.checked_sub(&self.x)) { - (Some(d), _) => { - dir = dir.up(); - d - }, - (_, Some(d)) => { - dir = dir.down(); - d - }, - (None, None) => { - unreachable!() - }, - }; - k.y = match (self.y.checked_sub(&rhs.y), rhs.y.checked_sub(&self.y)) { - (Some(d), _) => { - dir = dir.left(); - d - }, - (_, Some(d)) => { - dir = dir.right(); - d - }, - (None, None) => { - unreachable!() - }, - }; - (k, dir) + debug!("{:?} <> {:?}", self.x, rhs.x); + if self.x < rhs.x { + dir = dir.down(); + relative.x = rhs.x - self.x; + } else if self.x > rhs.x { + dir = dir.up(); + relative.x = self.x - rhs.x; + } + debug!("{:?} <> {:?}", self.x, rhs.x); + if self.y < rhs.y { + dir = dir.right(); + relative.y = rhs.y - self.y; + } else if self.y > rhs.y { + dir = dir.left(); + relative.y = self.y - rhs.y; + } + (relative, dir) } #[inline] @@ -202,9 +210,9 @@ impl Kartesian { } } -impl + Copy> Display for Kartesian { +impl Display for Kartesian { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("{}:{}", self.x + T::from(1), self.y + T::from(1))) + f.write_fmt(format_args!("{}:{}", self.x, self.y)) } } @@ -232,32 +240,22 @@ pub enum KartesianDirection { } impl KartesianDirection { - pub fn vector(self) -> Kartesian { + pub fn vector(self) -> Kartesian { Kartesian { x: match self { - Self::TopLeft | Self::Top | Self::TopRight => -1, - Self::Left | Self::None | Self::Right => 0, - Self::BottomLeft | Self::Bottom | Self::BottomRight => 1, + Self::Left | Self::None | Self::Right => T::zero(), + _ => T::one(), }, y: match self { - Self::TopLeft | Self::Left | Self::BottomLeft => -1, - Self::Top | Self::None | Self::Bottom => 0, - Self::TopRight | Self::Right | Self::BottomRight => 1, + Self::Top | Self::None | Self::Bottom => T::zero(), + _ => T::one(), }, } } + #[inline] pub fn vector_abs>(self) -> Kartesian { - Kartesian { - x: match self { - Self::Left | Self::None | Self::Right => From::from(0), - _ => From::from(1), - }, - y: match self { - Self::Top | Self::None | Self::Bottom => From::from(0), - _ => From::from(1), - }, - } + self.vector() } pub fn clockwise(self, diagonal: bool) -> Self { @@ -322,7 +320,7 @@ impl KartesianDirection { match self { Self::TopLeft | Self::Left | Self::BottomLeft => self, Self::Top => Self::TopLeft, - Self::None => Self::None, + Self::None => Self::Left, Self::Bottom => Self::BottomLeft, Self::TopRight => Self::Top, Self::Right => Self::None, @@ -399,9 +397,9 @@ impl Iterator for KartesianIterator { } match i { 0 => return Some(KartesianDirection::Right), - 1 => return Some(KartesianDirection::Top), + 1 => return Some(KartesianDirection::Bottom), 2 => return Some(KartesianDirection::Left), - 3 => return Some(KartesianDirection::Bottom), + 3 => return Some(KartesianDirection::Top), _ => i -= 4, } } @@ -499,4 +497,21 @@ mod test { let i: Vec<_> = KartesianDirection::iter(false, true, true).collect(); assert_eq!(i, test); } + + #[test] + fn test_diff_dir() { + const START: Kartesian = Kartesian::new(1, 1); + // Top Group + assert_eq!(KartesianDirection::TopLeft, START.diff(Kartesian::new(0, 0)).1); + assert_eq!(KartesianDirection::Top, START.diff(Kartesian::new(0, 1)).1); + assert_eq!(KartesianDirection::TopRight, START.diff(Kartesian::new(0, 2)).1); + // Same Line + assert_eq!(KartesianDirection::Left, START.diff(Kartesian::new(1, 0)).1); + assert_eq!(KartesianDirection::None, START.diff(Kartesian::new(1, 1)).1); + assert_eq!(KartesianDirection::Right, START.diff(Kartesian::new(1, 2)).1); + // Below/Bottom Line + assert_eq!(KartesianDirection::BottomLeft, START.diff(Kartesian::new(2, 0)).1); + assert_eq!(KartesianDirection::Bottom, START.diff(Kartesian::new(2, 1)).1); + assert_eq!(KartesianDirection::BottomRight, START.diff(Kartesian::new(2, 2)).1); + } } diff --git a/src/lib.rs b/src/lib.rs index 477470f..5bb743d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,7 @@ -use std::ops::{Add, AddAssign, DivAssign, Sub, SubAssign}; +use std::{ + ops::DivAssign, + time::{Duration, Instant}, +}; use num::*; @@ -31,8 +34,8 @@ pub fn matrix(zero: bool, diag: bool, non_diag: bool) -> Vec<(i32, i32)> { d } -pub fn numberlength + Copy>(n: T) -> u32 { - let divider = T::from(10); +pub fn numberlength_base + Copy, const BASE: u32>(n: T) -> u32 { + let divider = T::from(BASE); let mut num = n; let mut length = 0; loop { @@ -43,3 +46,22 @@ pub fn numberlength + Copy>(n: T) -> u32 { length += 1; } } + +#[inline] +pub fn numberlength + Copy>(n: T) -> u32 { + numberlength_base::(n) +} + +pub fn time_function T>(func: F) -> (Duration, T) { + let now = Instant::now(); + let o = func(); + (now.elapsed(), o) +} + +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)] +pub enum ExtendedOption { + #[default] + Unset, + Some(T), + None, +} diff --git a/src/strings.rs b/src/strings.rs index 9392dda..9f50e08 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -18,9 +18,7 @@ pub fn char_to_num>(c: char) -> T { } } -pub fn parsenumber + Pow + AddAssign + Copy>( - input: &str, -) -> T { +pub fn parsenumber + Pow + AddAssign + Copy>(input: &str) -> T { const MAX_POWER: u32 = 32; let base = T::from(10); let mut output: T = T::from(0); @@ -52,60 +50,60 @@ pub fn get_string_numbers(input: &str) -> Vec { "1" => { output.push(1); continue; - } + }, "2" => { output.push(2); continue; - } + }, "3" => { output.push(3); continue; - } + }, "4" => { output.push(4); continue; - } + }, "5" => { output.push(5); continue; - } + }, "6" => { output.push(6); continue; - } + }, "7" => { output.push(7); continue; - } + }, "8" => { output.push(8); continue; - } + }, "9" => { output.push(9); continue; - } + }, "0" => { output.push(0); continue; - } - _ => {} + }, + _ => {}, } if (i + 3) <= len { match input.get(i..i + 3).unwrap() { "one" => { output.push(1); continue; - } + }, "two" => { output.push(2); continue; - } + }, "six" => { output.push(6); continue; - } - _ => {} + }, + _ => {}, }; } if (i + 4) <= len { @@ -113,20 +111,20 @@ pub fn get_string_numbers(input: &str) -> Vec { "four" => { output.push(4); continue; - } + }, "five" => { output.push(5); continue; - } + }, "nine" => { output.push(9); continue; - } + }, "zero" => { output.push(0); continue; - } - _ => {} + }, + _ => {}, }; } if (i + 5) <= len { @@ -134,16 +132,16 @@ pub fn get_string_numbers(input: &str) -> Vec { "three" => { output.push(3); continue; - } + }, "seven" => { output.push(7); continue; - } + }, "eight" => { output.push(8); continue; - } - _ => {} + }, + _ => {}, }; } } @@ -156,10 +154,5 @@ pub fn line_to_char(line: &str) -> Vec { } pub fn convert_to_array T, const S: char>(input: &str, func: F) -> Vec { - input - .trim() - .split(S) - .map(str::trim_ascii) - .map(func) - .collect() + input.trim().split(S).map(str::trim_ascii).map(func).collect() }