added inclimple day 12
Dieser Commit ist enthalten in:
Ursprung
78327e8658
Commit
50f451efd2
11 geänderte Dateien mit 162 neuen und 114 gelöschten Zeilen
1
examples/2024/12.txt
Softlink
1
examples/2024/12.txt
Softlink
|
@ -0,0 +1 @@
|
|||
12c.txt
|
4
examples/2024/12a.txt
Normale Datei
4
examples/2024/12a.txt
Normale Datei
|
@ -0,0 +1,4 @@
|
|||
AAAA
|
||||
BBCD
|
||||
BBCC
|
||||
EEEC
|
5
examples/2024/12b.txt
Normale Datei
5
examples/2024/12b.txt
Normale Datei
|
@ -0,0 +1,5 @@
|
|||
OOOOO
|
||||
OXOXO
|
||||
OOOOO
|
||||
OXOXO
|
||||
OOOOO
|
10
examples/2024/12c.txt
Normale Datei
10
examples/2024/12c.txt
Normale Datei
|
@ -0,0 +1,10 @@
|
|||
RRRRIICCFF
|
||||
RRRRIICCCF
|
||||
VVRRRCCFFF
|
||||
VVRCCCJFFF
|
||||
VVVVCJJCFE
|
||||
VVIVCCJJEE
|
||||
VVIIICJJEE
|
||||
MIIIIIJJEE
|
||||
MIIISIJEEE
|
||||
MMMISSJEEE
|
5
examples/2024/12d.txt
Normale Datei
5
examples/2024/12d.txt
Normale Datei
|
@ -0,0 +1,5 @@
|
|||
EEEEE
|
||||
EXXXX
|
||||
EEEEE
|
||||
EXXXX
|
||||
EEEEE
|
6
examples/2024/12f.txt
Normale Datei
6
examples/2024/12f.txt
Normale Datei
|
@ -0,0 +1,6 @@
|
|||
AAAAAA
|
||||
AAABBA
|
||||
AAABBA
|
||||
ABBAAA
|
||||
ABBAAA
|
||||
AAAAAA
|
|
@ -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<T: Spanned>(item: T) -> Result<String> {
|
|||
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<String> {
|
|||
} 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]
|
||||
|
|
|
@ -60,4 +60,7 @@ impl<T: Integer> Euclidian<T> {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
fn test() {
|
||||
Euclidian { x: 0, y: 0, z: 0 };
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T: Integer> {
|
||||
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>> {
|
||||
let mut new = Kartesian::default();
|
||||
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) {
|
||||
let mut k = Kartesian::<T>::default();
|
||||
let mut relative = Kartesian::<T>::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)) => {
|
||||
debug!("{:?} <> {:?}", self.x, rhs.x);
|
||||
if self.x < rhs.x {
|
||||
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)) => {
|
||||
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();
|
||||
d
|
||||
},
|
||||
(None, None) => {
|
||||
unreachable!()
|
||||
},
|
||||
};
|
||||
(k, dir)
|
||||
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<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 {
|
||||
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<i16> {
|
||||
pub fn vector<T: Integer>(self) -> Kartesian<T> {
|
||||
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<T: Integer + From<u8>>(self) -> Kartesian<T> {
|
||||
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<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);
|
||||
}
|
||||
}
|
||||
|
|
28
src/lib.rs
28
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<T: Integer + DivAssign + From<u32> + Copy>(n: T) -> u32 {
|
||||
let divider = T::from(10);
|
||||
pub fn numberlength_base<T: Integer + DivAssign + From<u32> + 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<T: Integer + DivAssign + From<u32> + Copy>(n: T) -> u32 {
|
|||
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,
|
||||
}
|
||||
|
|
|
@ -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>(
|
||||
input: &str,
|
||||
) -> T {
|
||||
pub fn parsenumber<T: Integer + From<u32> + Pow<u32, Output = T> + 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<u32> {
|
|||
"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<u32> {
|
|||
"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<u32> {
|
|||
"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<char> {
|
|||
}
|
||||
|
||||
pub fn convert_to_array<T, F: FnMut(&str) -> T, const S: char>(input: &str, func: F) -> Vec<T> {
|
||||
input
|
||||
.trim()
|
||||
.split(S)
|
||||
.map(str::trim_ascii)
|
||||
.map(func)
|
||||
.collect()
|
||||
input.trim().split(S).map(str::trim_ascii).map(func).collect()
|
||||
}
|
||||
|
|
Laden …
Tabelle hinzufügen
In neuem Issue referenzieren