added inclimple day 12

Dieser Commit ist enthalten in:
Sebastian Tobie 2024-12-14 11:42:26 +01:00
Ursprung 78327e8658
Commit 50f451efd2
11 geänderte Dateien mit 162 neuen und 114 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1 @@
12c.txt

4
examples/2024/12a.txt Normale Datei
Datei anzeigen

@ -0,0 +1,4 @@
AAAA
BBCD
BBCC
EEEC

5
examples/2024/12b.txt Normale Datei
Datei anzeigen

@ -0,0 +1,5 @@
OOOOO
OXOXO
OOOOO
OXOXO
OOOOO

10
examples/2024/12c.txt Normale Datei
Datei anzeigen

@ -0,0 +1,10 @@
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE

5
examples/2024/12d.txt Normale Datei
Datei anzeigen

@ -0,0 +1,5 @@
EEEEE
EXXXX
EEEEE
EXXXX
EEEEE

6
examples/2024/12f.txt Normale Datei
Datei anzeigen

@ -0,0 +1,6 @@
AAAAAA
AAABBA
AAABBA
ABBAAA
ABBAAA
AAAAAA

Datei anzeigen

@ -22,11 +22,7 @@ struct IncludeData {
impl Default for IncludeData { impl Default for IncludeData {
fn default() -> Self { fn default() -> Self {
IncludeData { IncludeData { var: VAR_NAME.into(), year: None, day: None }
var: VAR_NAME.into(),
year: None,
day: None,
}
} }
} }
@ -37,14 +33,8 @@ fn get_text<T: Spanned>(item: T) -> Result<String> {
None => { None => {
let start = span.start(); let start = span.start();
let end = span.end(); let end = span.end();
Err(Error::new( Err(Error::new(span, format!("Failed to get sourcetext for {}:{}-{}:{}", start.line, start.column, end.line, end.column,)))
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<String> {
} else { } else {
None None
} }
} },
Err(_) => None, Err(_) => None,
} }
} }
@ -113,15 +103,12 @@ pub fn include_data(data: TokenStream) -> TokenStream {
#[doc = #comment] #[doc = #comment]
const #ident: &str = include_str!(#p).trim_ascii(); const #ident: &str = include_str!(#p).trim_ascii();
} }
.into() .into();
} },
None => comment = "failed to get data from the paths".into(), None => comment = "failed to get data from the paths".into(),
} }
} else { } else {
comment = format!( comment = format!("Failed to get the year({:?}) or day({:?}) falling back to default", input.year, input.day);
"Failed to get the year({:?}) or day({:?}) falling back to default",
input.year, input.day
);
} }
quote! { quote! {
#[doc = #comment] #[doc = #comment]
@ -148,15 +135,12 @@ pub fn include_example(data: TokenStream) -> TokenStream {
#[doc = #comment] #[doc = #comment]
const #ident: &str = include_str!(#p).trim_ascii(); const #ident: &str = include_str!(#p).trim_ascii();
} }
.into() .into();
} },
None => comment = "failed to get data from the path".into(), None => comment = "failed to get data from the path".into(),
} }
} else { } else {
comment = format!( comment = format!("Failed to get the year({:?}) or day({:?}) falling back to default", input.year, input.day);
"Failed to get the year({:?}) or day({:?}) falling back to default",
input.year, input.day
);
} }
quote! { quote! {
#[doc = #comment] #[doc = #comment]

Datei anzeigen

@ -60,4 +60,7 @@ impl<T: Integer> Euclidian<T> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
fn test() {
Euclidian { x: 0, y: 0, z: 0 };
}
} }

Datei anzeigen

@ -1,5 +1,11 @@
use log::debug;
use num::*; use num::*;
use std::{fmt::Display, ops::*}; use std::{
fmt::{Debug, Display},
ops::*,
};
pub type KD = KartesianDirection;
pub trait MaximumFromMap<T: Integer> { pub trait MaximumFromMap<T: Integer> {
fn maximum<U>(map: &Vec<Vec<U>>) -> Kartesian<T>; fn maximum<U>(map: &Vec<Vec<U>>) -> Kartesian<T>;
@ -83,7 +89,19 @@ impl<T: Integer> Kartesian<T> {
} }
} }
impl<T: Integer + CheckedAdd + CheckedSub + Default> Kartesian<T> { impl Kartesian<usize> {
pub fn get_value<T: Copy>(self, map: &Vec<Vec<T>>) -> Option<T> {
if map.len() > self.x {
if map[self.x].len() > self.y {
return Some(map[self.x][self.y]);
}
}
None
}
}
impl<T: Integer + Eq + Debug + CheckedAdd + CheckedSub + Default + Copy> Kartesian<T> {
pub fn checked_add_max(self, rhs: Kartesian<T>, max: Kartesian<T>) -> Option<Kartesian<T>> { pub fn checked_add_max(self, rhs: Kartesian<T>, max: Kartesian<T>) -> Option<Kartesian<T>> {
let mut new = Kartesian::default(); let mut new = Kartesian::default();
new.x = match self.x.checked_add(&rhs.x) { new.x = match self.x.checked_add(&rhs.x) {
@ -120,35 +138,25 @@ impl<T: Integer + CheckedAdd + CheckedSub + Default> Kartesian<T> {
} }
pub fn diff(self, rhs: Kartesian<T>) -> (Kartesian<T>, KartesianDirection) { pub fn diff(self, rhs: Kartesian<T>) -> (Kartesian<T>, KartesianDirection) {
let mut k = Kartesian::<T>::default(); let mut relative = Kartesian::<T>::default();
let mut dir = KartesianDirection::None; let mut dir = KartesianDirection::None;
k.x = match (self.x.checked_sub(&rhs.x), rhs.x.checked_sub(&self.x)) { debug!("{:?} <> {:?}", self.x, rhs.x);
(Some(d), _) => { if self.x < rhs.x {
dir = dir.up();
d
},
(_, Some(d)) => {
dir = dir.down(); dir = dir.down();
d relative.x = rhs.x - self.x;
}, } else if self.x > rhs.x {
(None, None) => { dir = dir.up();
unreachable!() relative.x = self.x - rhs.x;
}, }
}; debug!("{:?} <> {:?}", self.x, rhs.x);
k.y = match (self.y.checked_sub(&rhs.y), rhs.y.checked_sub(&self.y)) { if self.y < rhs.y {
(Some(d), _) => {
dir = dir.left();
d
},
(_, Some(d)) => {
dir = dir.right(); dir = dir.right();
d relative.y = rhs.y - self.y;
}, } else if self.y > rhs.y {
(None, None) => { dir = dir.left();
unreachable!() relative.y = self.y - rhs.y;
}, }
}; (relative, dir)
(k, dir)
} }
#[inline] #[inline]
@ -202,9 +210,9 @@ impl<T: Integer + CheckedAdd + CheckedSub + Default> Kartesian<T> {
} }
} }
impl<T: Integer + Display + From<u8> + Copy> Display for Kartesian<T> { impl<T: Integer + Display + Copy> Display for Kartesian<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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 { impl KartesianDirection {
pub fn vector(self) -> Kartesian<i16> { pub fn vector<T: Integer>(self) -> Kartesian<T> {
Kartesian { Kartesian {
x: match self { x: match self {
Self::TopLeft | Self::Top | Self::TopRight => -1, Self::Left | Self::None | Self::Right => T::zero(),
Self::Left | Self::None | Self::Right => 0, _ => T::one(),
Self::BottomLeft | Self::Bottom | Self::BottomRight => 1,
}, },
y: match self { y: match self {
Self::TopLeft | Self::Left | Self::BottomLeft => -1, Self::Top | Self::None | Self::Bottom => T::zero(),
Self::Top | Self::None | Self::Bottom => 0, _ => T::one(),
Self::TopRight | Self::Right | Self::BottomRight => 1,
}, },
} }
} }
#[inline]
pub fn vector_abs<T: Integer + From<u8>>(self) -> Kartesian<T> { pub fn vector_abs<T: Integer + From<u8>>(self) -> Kartesian<T> {
Kartesian { self.vector()
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),
},
}
} }
pub fn clockwise(self, diagonal: bool) -> Self { pub fn clockwise(self, diagonal: bool) -> Self {
@ -322,7 +320,7 @@ impl KartesianDirection {
match self { match self {
Self::TopLeft | Self::Left | Self::BottomLeft => self, Self::TopLeft | Self::Left | Self::BottomLeft => self,
Self::Top => Self::TopLeft, Self::Top => Self::TopLeft,
Self::None => Self::None, Self::None => Self::Left,
Self::Bottom => Self::BottomLeft, Self::Bottom => Self::BottomLeft,
Self::TopRight => Self::Top, Self::TopRight => Self::Top,
Self::Right => Self::None, Self::Right => Self::None,
@ -399,9 +397,9 @@ impl Iterator for KartesianIterator {
} }
match i { match i {
0 => return Some(KartesianDirection::Right), 0 => return Some(KartesianDirection::Right),
1 => return Some(KartesianDirection::Top), 1 => return Some(KartesianDirection::Bottom),
2 => return Some(KartesianDirection::Left), 2 => return Some(KartesianDirection::Left),
3 => return Some(KartesianDirection::Bottom), 3 => return Some(KartesianDirection::Top),
_ => i -= 4, _ => i -= 4,
} }
} }
@ -499,4 +497,21 @@ mod test {
let i: Vec<_> = KartesianDirection::iter(false, true, true).collect(); let i: Vec<_> = KartesianDirection::iter(false, true, true).collect();
assert_eq!(i, test); assert_eq!(i, test);
} }
#[test]
fn test_diff_dir() {
const START: Kartesian<u8> = 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);
}
} }

Datei anzeigen

@ -1,4 +1,7 @@
use std::ops::{Add, AddAssign, DivAssign, Sub, SubAssign}; use std::{
ops::DivAssign,
time::{Duration, Instant},
};
use num::*; use num::*;
@ -31,8 +34,8 @@ pub fn matrix(zero: bool, diag: bool, non_diag: bool) -> Vec<(i32, i32)> {
d d
} }
pub fn numberlength<T: Integer + DivAssign + From<u32> + Copy>(n: T) -> u32 { pub fn numberlength_base<T: Integer + DivAssign + From<u32> + Copy, const BASE: u32>(n: T) -> u32 {
let divider = T::from(10); let divider = T::from(BASE);
let mut num = n; let mut num = n;
let mut length = 0; let mut length = 0;
loop { loop {
@ -43,3 +46,22 @@ pub fn numberlength<T: Integer + DivAssign + From<u32> + Copy>(n: T) -> u32 {
length += 1; length += 1;
} }
} }
#[inline]
pub fn numberlength<T: Integer + DivAssign + From<u32> + Copy>(n: T) -> u32 {
numberlength_base::<T, 10>(n)
}
pub fn time_function<T, F: FnOnce() -> 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<T> {
#[default]
Unset,
Some(T),
None,
}

Datei anzeigen

@ -18,9 +18,7 @@ pub fn char_to_num<T: Integer + From<u32>>(c: char) -> T {
} }
} }
pub fn parsenumber<T: Integer + From<u32> + Pow<u32, Output = T> + AddAssign + Copy>( pub fn parsenumber<T: Integer + From<u32> + Pow<u32, Output = T> + AddAssign + Copy>(input: &str) -> T {
input: &str,
) -> T {
const MAX_POWER: u32 = 32; const MAX_POWER: u32 = 32;
let base = T::from(10); let base = T::from(10);
let mut output: T = T::from(0); let mut output: T = T::from(0);
@ -52,60 +50,60 @@ pub fn get_string_numbers(input: &str) -> Vec<u32> {
"1" => { "1" => {
output.push(1); output.push(1);
continue; continue;
} },
"2" => { "2" => {
output.push(2); output.push(2);
continue; continue;
} },
"3" => { "3" => {
output.push(3); output.push(3);
continue; continue;
} },
"4" => { "4" => {
output.push(4); output.push(4);
continue; continue;
} },
"5" => { "5" => {
output.push(5); output.push(5);
continue; continue;
} },
"6" => { "6" => {
output.push(6); output.push(6);
continue; continue;
} },
"7" => { "7" => {
output.push(7); output.push(7);
continue; continue;
} },
"8" => { "8" => {
output.push(8); output.push(8);
continue; continue;
} },
"9" => { "9" => {
output.push(9); output.push(9);
continue; continue;
} },
"0" => { "0" => {
output.push(0); output.push(0);
continue; continue;
} },
_ => {} _ => {},
} }
if (i + 3) <= len { if (i + 3) <= len {
match input.get(i..i + 3).unwrap() { match input.get(i..i + 3).unwrap() {
"one" => { "one" => {
output.push(1); output.push(1);
continue; continue;
} },
"two" => { "two" => {
output.push(2); output.push(2);
continue; continue;
} },
"six" => { "six" => {
output.push(6); output.push(6);
continue; continue;
} },
_ => {} _ => {},
}; };
} }
if (i + 4) <= len { if (i + 4) <= len {
@ -113,20 +111,20 @@ pub fn get_string_numbers(input: &str) -> Vec<u32> {
"four" => { "four" => {
output.push(4); output.push(4);
continue; continue;
} },
"five" => { "five" => {
output.push(5); output.push(5);
continue; continue;
} },
"nine" => { "nine" => {
output.push(9); output.push(9);
continue; continue;
} },
"zero" => { "zero" => {
output.push(0); output.push(0);
continue; continue;
} },
_ => {} _ => {},
}; };
} }
if (i + 5) <= len { if (i + 5) <= len {
@ -134,16 +132,16 @@ pub fn get_string_numbers(input: &str) -> Vec<u32> {
"three" => { "three" => {
output.push(3); output.push(3);
continue; continue;
} },
"seven" => { "seven" => {
output.push(7); output.push(7);
continue; continue;
} },
"eight" => { "eight" => {
output.push(8); output.push(8);
continue; continue;
} },
_ => {} _ => {},
}; };
} }
} }
@ -156,10 +154,5 @@ pub fn line_to_char(line: &str) -> Vec<char> {
} }
pub fn convert_to_array<T, F: FnMut(&str) -> T, const S: char>(input: &str, func: F) -> Vec<T> { pub fn convert_to_array<T, F: FnMut(&str) -> T, const S: char>(input: &str, func: F) -> Vec<T> {
input input.trim().split(S).map(str::trim_ascii).map(func).collect()
.trim()
.split(S)
.map(str::trim_ascii)
.map(func)
.collect()
} }