Commits vergleichen

...

4 Commits

Autor SHA1 Nachricht Datum
55b3cd16f7 day 19 part1 2024-12-19 06:49:04 +01:00
fef542ba78 some formatting 2024-12-18 21:59:56 +01:00
ea6f9170eb updated some code 2024-12-18 21:59:43 +01:00
5f88cdc3b4 updated the lib 2024-12-18 21:56:41 +01:00
4 geänderte Dateien mit 96 neuen und 18 gelöschten Zeilen

Datei anzeigen

@ -26,18 +26,18 @@ fn new_score(old_score: u32, old: KD, new: KD) -> u32 {
} }
} }
fn sorter(a: &(Kartesian<usize>, KartesianDirection, u32, Kartesian<usize>), b: &(Kartesian<usize>, KartesianDirection, u32, Kartesian<usize>)) -> Ordering { fn sorter(a: &(Kartesian<usize>, KartesianDirection, u32), b: &(Kartesian<usize>, KartesianDirection, u32)) -> Ordering {
a.2.cmp(&b.2).reverse() a.2.cmp(&b.2).reverse()
} }
fn search_end(map: &Map<char>, startpos: Kartesian<usize>, last_dir: KartesianDirection) -> u32 { fn search_end(map: &Map<char>, startpos: Kartesian<usize>, last_dir: KartesianDirection) -> u32 {
let mut posmap: HashMap<Kartesian<usize>, u32> = HashMap::new(); let mut posmap: HashMap<Kartesian<usize>, u32> = HashMap::new();
let mut branches = Vec::with_capacity(200); let mut branches = Vec::with_capacity(200);
branches.push((startpos, last_dir, 0, startpos)); branches.push((startpos, last_dir, 0));
let mut vmap = map.clone(); let mut vmap = map.clone();
let mut best = u32::MAX; let mut best = u32::MAX;
loop { loop {
let (newpos, old_dir, score, oldpos) = match branches.pop() { let (newpos, old_dir, score) = match branches.pop() {
Some(b) => b, Some(b) => b,
None => break, None => break,
}; };
@ -72,7 +72,7 @@ fn search_end(map: &Map<char>, startpos: Kartesian<usize>, last_dir: KartesianDi
}, },
SPACE | START => { SPACE | START => {
let added = new_score(score, old_dir, dir); let added = new_score(score, old_dir, dir);
branches.push((after_move, dir, added, newpos)); branches.push((after_move, dir, added));
}, },
_ => unreachable!(), _ => unreachable!(),
} }

48
days/2024/19.rs Normale Datei
Datei anzeigen

@ -0,0 +1,48 @@
use advent_of_code::strings::convert_to_array;
#[allow(unused_imports)]
use advent_of_code_macros::{include_data, include_example};
include_data!(DATA 2024 19);
fn test_pattern(pattern: String, parts: Vec<String>) -> bool {
false
}
fn main() {
let (patterndata, designdata) = DATA.split_once("\n\n").unwrap();
let mut designs = convert_to_array::<_, _, '\n'>(designdata, str::to_string);
let patterns = convert_to_array::<_, _, ','>(patterndata, str::to_string);
designs.sort();
let mut possible = 0;
for design in designs {
let mut candidates = vec![design];
'designloop :loop {
let mut new = Vec::with_capacity(candidates.len());
for possibility in candidates {
for part in patterns.clone() {
if possibility.starts_with(part.as_str()) {
if possibility.len() == part.len() {
println!("Found design");
possible+=1;
break 'designloop;
}
new.push(possibility[part.len()..].to_owned());
}
}
}
new.sort();
new.dedup();
candidates = new;
if candidates.len() == 0 {
break;
}
}
}
println!("There are {} possible designs", possible);
}
#[cfg(test)]
mod test {
use super::*;
}

10
examples/2024/19.txt Normale Datei
Datei anzeigen

@ -0,0 +1,10 @@
r, wr, b, g, bwu, rb, gb, br
brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb

Datei anzeigen

@ -1,8 +1,8 @@
use log::debug; use log::debug;
use num::{traits::*, *}; use num::{integer::*, traits::*};
use std::{ use std::{
fmt::{Debug, Display}, fmt::{Debug, Display},
ops::Rem, ops::*,
}; };
pub mod direction; pub mod direction;
@ -46,18 +46,6 @@ fn wrap<T: Integer + Rem + MaximumValue + From<u32> + Copy + MaximumValue + Disp
} }
} }
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 + CheckedBasicMath + Default + Copy> Kartesian<T> { impl<T: Integer + Eq + Debug + CheckedBasicMath + 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();
@ -117,6 +105,7 @@ impl<T: Integer + Eq + Debug + CheckedBasicMath + Default + Copy> Kartesian<T> {
} }
#[inline] #[inline]
#[must_use]
pub fn move_dir(self, vector: Kartesian<T>, dir: KartesianDirection) -> Option<Kartesian<T>> { pub fn move_dir(self, vector: Kartesian<T>, dir: KartesianDirection) -> Option<Kartesian<T>> {
let mut new = self; let mut new = self;
match dir { match dir {
@ -153,6 +142,7 @@ impl<T: Integer + Eq + Debug + CheckedBasicMath + Default + Copy> Kartesian<T> {
} }
#[inline] #[inline]
#[must_use]
pub fn move_dir_max(self, vector: Kartesian<T>, dir: KartesianDirection, max: Kartesian<T>) -> Option<Kartesian<T>> { pub fn move_dir_max(self, vector: Kartesian<T>, dir: KartesianDirection, max: Kartesian<T>) -> Option<Kartesian<T>> {
match self.move_dir(vector, dir) { match self.move_dir(vector, dir) {
None => None, None => None,
@ -167,7 +157,20 @@ impl<T: Integer + Eq + Debug + CheckedBasicMath + Default + Copy> 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 + Unsigned + CheckedBasicMath> Kartesian<T> { impl<T: Integer + Unsigned + CheckedBasicMath> Kartesian<T> {
#[must_use]
pub fn move_velocity(self, velocity: Velocity<T>, max: Kartesian<T>) -> Option<Kartesian<T>> { pub fn move_velocity(self, velocity: Velocity<T>, max: Kartesian<T>) -> Option<Kartesian<T>> {
if velocity.direction == KD::None { if velocity.direction == KD::None {
return Some(self); return Some(self);
@ -207,6 +210,23 @@ impl<T: Integer + Unsigned + CheckedBasicMath> Kartesian<T> {
} }
} }
impl<T: Integer + From<u16> + Copy + Add + Roots + Pow<T, Output = T>> Kartesian<T> {
pub fn distance(self, other: Self) -> T {
let power = T::from(2);
let x = if self.x > other.x {
self.x - other.x
} else {
other.x - self.x
};
let y = if self.y > other.y {
self.y - other.y
} else {
other.y - self.y
};
(x.pow(power) + y.pow(power)).sqrt()
}
}
impl<T: Integer + Display + Unsigned + WrappingAdd + WrappingSub + MaximumValue + From<u32> + Copy> Kartesian<T> { impl<T: Integer + Display + Unsigned + WrappingAdd + WrappingSub + MaximumValue + From<u32> + Copy> Kartesian<T> {
pub fn wrapping_move_velocity(self, velocity: Velocity<T>, max: Kartesian<T>) -> Kartesian<T> { pub fn wrapping_move_velocity(self, velocity: Velocity<T>, max: Kartesian<T>) -> Kartesian<T> {
Kartesian { Kartesian {