formatted some code and renamed nonndiagonal to straight
Dieser Commit ist enthalten in:
Ursprung
50f451efd2
Commit
f3339b38e1
7 geänderte Dateien mit 67 neuen und 106 gelöschten Zeilen
|
@ -42,11 +42,7 @@ impl Round {
|
|||
ok
|
||||
}
|
||||
fn power(&self) -> u64 {
|
||||
let mut sack = Sack {
|
||||
reds: 0,
|
||||
greens: 0,
|
||||
blues: 0,
|
||||
};
|
||||
let mut sack = Sack { reds: 0, greens: 0, blues: 0 };
|
||||
for take in self.takes.clone() {
|
||||
if take.red > sack.reds {
|
||||
sack.reds = take.red
|
||||
|
@ -70,11 +66,7 @@ impl From<&str> for Round {
|
|||
takes: Vec::<Take>::new(),
|
||||
};
|
||||
for taking in value.get(doublecolon + 1..).unwrap().split(";") {
|
||||
let mut take = Take {
|
||||
red: 0,
|
||||
green: 0,
|
||||
blue: 0,
|
||||
};
|
||||
let mut take = Take { red: 0, green: 0, blue: 0 };
|
||||
for color in taking.split(',').map(str::trim) {
|
||||
let mut i = color.splitn(2, char::is_whitespace);
|
||||
let amount = parsenumber(i.next().unwrap());
|
||||
|
@ -96,11 +88,7 @@ fn main() {
|
|||
for game in DATA.split('\n') {
|
||||
gamerounds.push(game.into());
|
||||
}
|
||||
let sack = Sack {
|
||||
reds: 12,
|
||||
greens: 13,
|
||||
blues: 14,
|
||||
};
|
||||
let sack = Sack { reds: 12, greens: 13, blues: 14 };
|
||||
let mut sum = 0;
|
||||
for round in gamerounds.clone() {
|
||||
if round.possible(sack) {
|
||||
|
|
|
@ -7,10 +7,7 @@ include_data!(DATA 2024 01);
|
|||
pub fn splitspace(input: &str) -> (u32, u32) {
|
||||
println!("{}", input);
|
||||
let mut output = input.split_ascii_whitespace();
|
||||
(
|
||||
parsenumber(output.next().unwrap()),
|
||||
parsenumber(output.next().unwrap()),
|
||||
)
|
||||
(parsenumber(output.next().unwrap()), parsenumber(output.next().unwrap()))
|
||||
}
|
||||
|
||||
fn distance(leftlist: &Vec<u32>, rightlist: &Vec<u32>) -> u32 {
|
||||
|
|
|
@ -34,8 +34,8 @@ fn main() {
|
|||
let a = parsenumber::<u32>(capture.name("i").unwrap().as_str());
|
||||
let b = parsenumber::<u32>(capture.name("j").unwrap().as_str());
|
||||
sum += a * b
|
||||
}
|
||||
(_, _) => {}
|
||||
},
|
||||
(_, _) => {},
|
||||
}
|
||||
}
|
||||
println!("The Cleaned with check output is: {}", sum);
|
||||
|
|
|
@ -18,23 +18,13 @@ struct DirectionalKartesian<T: Integer> {
|
|||
}
|
||||
impl<T: Integer> From<(Kartesian<T>, KartesianDirection)> for DirectionalKartesian<T> {
|
||||
fn from(value: (Kartesian<T>, KartesianDirection)) -> Self {
|
||||
DirectionalKartesian {
|
||||
x: value.0.x,
|
||||
y: value.0.y,
|
||||
dir: value.1,
|
||||
}
|
||||
DirectionalKartesian { x: value.0.x, y: value.0.y, dir: value.1 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Integer + Copy> Into<(Kartesian<T>, KartesianDirection)> for &DirectionalKartesian<T> {
|
||||
fn into(self) -> (Kartesian<T>, KartesianDirection) {
|
||||
(
|
||||
Kartesian {
|
||||
x: self.x,
|
||||
y: self.y,
|
||||
},
|
||||
self.dir,
|
||||
)
|
||||
(Kartesian { x: self.x, y: self.y }, self.dir)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,19 +52,15 @@ fn find_guard(map: Table) -> Kartesian<i32> {
|
|||
fn get_path(map: Table) -> Vec<DirectionalKartesian<i32>> {
|
||||
let mut position = (find_guard(map.clone()), KartesianDirection::Top);
|
||||
let maximum = Kartesian::new(map.len() as i32, map[0].len() as i32);
|
||||
debug!(
|
||||
"Guard is on coordinate {}:{}",
|
||||
position.0.x + 1,
|
||||
position.0.y + 1
|
||||
);
|
||||
debug!("Guard is on coordinate {}:{}", position.0.x + 1, position.0.y + 1);
|
||||
let mut passed = Vec::new();
|
||||
passed.push(position.into());
|
||||
loop {
|
||||
position.0 = match position.0.checked_add_max(position.1.vector(), maximum) {
|
||||
position.0 = match position.0.checked_add_max(position.1.vector().into(), maximum) {
|
||||
None => {
|
||||
debug!("Guard left the space after {} steps", passed.len());
|
||||
return passed;
|
||||
}
|
||||
},
|
||||
Some(pos) => pos,
|
||||
};
|
||||
if map[position.0.x as usize][position.0.y as usize] != '#' {
|
||||
|
@ -82,33 +68,26 @@ fn get_path(map: Table) -> Vec<DirectionalKartesian<i32>> {
|
|||
continue;
|
||||
}
|
||||
position.0 -= position.1.vector();
|
||||
debug!(
|
||||
"Guard turned right on {}:{}",
|
||||
position.0.x + 1,
|
||||
position.0.y + 1
|
||||
);
|
||||
debug!("Guard turned right on {}:{}", position.0.x + 1, position.0.y + 1);
|
||||
position.1 = position.1.clockwise(false);
|
||||
}
|
||||
}
|
||||
|
||||
fn solver1(input: &str) -> usize {
|
||||
let map = convert_to_array(input, line_to_char);
|
||||
let map = convert_to_array::<_, _, '\n'>(input, line_to_char);
|
||||
let mut path = get_path(map);
|
||||
path.sort();
|
||||
path.dedup_by_key(|p| (p.x, p.y));
|
||||
path.len()
|
||||
}
|
||||
|
||||
fn is_looping(
|
||||
path: Vec<DirectionalKartesian<i32>>,
|
||||
start_point: DirectionalKartesian<i32>,
|
||||
) -> bool {
|
||||
fn is_looping(_path: Vec<DirectionalKartesian<i32>>, _start_point: DirectionalKartesian<i32>) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[allow(unused_variables, unused_mut)]
|
||||
fn solver2(input: &str) -> usize {
|
||||
let map = convert_to_array(input, line_to_char);
|
||||
let map = convert_to_array::<_, _, '\n'>(input, line_to_char);
|
||||
let maximum = Kartesian::new(map.len() as i32, map[0].len() as i32);
|
||||
let mut path = get_path(map.clone());
|
||||
let mut p = path.iter();
|
||||
|
@ -123,10 +102,7 @@ fn solver2(input: &str) -> usize {
|
|||
currentpoint = point.into();
|
||||
currentpoint.1 = currentpoint.1.clockwise(false);
|
||||
loop {
|
||||
currentpoint.0 = match currentpoint
|
||||
.0
|
||||
.checked_add_max(currentpoint.1.vector(), maximum)
|
||||
{
|
||||
currentpoint.0 = match currentpoint.0.checked_add_max(currentpoint.1.vector(), maximum) {
|
||||
None => break,
|
||||
Some(p) => p,
|
||||
};
|
||||
|
@ -145,14 +121,8 @@ fn solver2(input: &str) -> usize {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
println!(
|
||||
"Guard left the space after walking through {} distinct positions",
|
||||
solver1(DATA)
|
||||
);
|
||||
println!(
|
||||
"There are {} positions that are good for loops",
|
||||
solver2(DATA)
|
||||
);
|
||||
println!("Guard left the space after walking through {} distinct positions", solver1(DATA));
|
||||
println!("There are {} positions that are good for loops", solver2(DATA));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -19,27 +19,14 @@ enum Operation {
|
|||
}
|
||||
|
||||
impl Operation {
|
||||
fn op<
|
||||
T: Integer
|
||||
+ Add
|
||||
+ Mul
|
||||
+ Pow<u32, Output = T>
|
||||
+ std::ops::DivAssign
|
||||
+ AddAssign
|
||||
+ From<u32>
|
||||
+ Copy,
|
||||
>(
|
||||
self,
|
||||
a: T,
|
||||
b: T,
|
||||
) -> T {
|
||||
fn op<T: Integer + Add + Mul + Pow<u32, Output = T> + std::ops::DivAssign + AddAssign + From<u32> + Copy>(self, a: T, b: T) -> T {
|
||||
match self {
|
||||
Operation::Add => a + b,
|
||||
Operation::Multiply => a * b,
|
||||
Operation::Concat => {
|
||||
let ta = a * T::from(10).pow(numberlength(b));
|
||||
ta + b
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,10 +34,7 @@ impl Operation {
|
|||
fn get_data(line: &str) -> (u64, Vec<u64>) {
|
||||
let (result, numbers) = line.split_once(':').unwrap();
|
||||
|
||||
(
|
||||
parsenumber(result.trim()),
|
||||
Vec::from(convert_to_array::<_, _, ' '>(numbers, parsenumber)),
|
||||
)
|
||||
(parsenumber(result.trim()), Vec::from(convert_to_array::<_, _, ' '>(numbers, parsenumber)))
|
||||
}
|
||||
|
||||
fn test_equation(result: u64, numbers: Vec<u64>, ops: Vec<Operation>) -> bool {
|
||||
|
@ -87,7 +71,11 @@ fn get_ops(len: usize, conf: usize) -> Vec<Operation> {
|
|||
|
||||
fn get_ops_with_concat(len: u32) -> Vec<Vec<Operation>> {
|
||||
let mut ops = Vec::with_capacity(3u64.saturating_pow(len) as usize);
|
||||
for i in [Operation::Add, Operation::Concat, Operation::Multiply] {
|
||||
for i in [
|
||||
Operation::Add,
|
||||
Operation::Concat,
|
||||
Operation::Multiply,
|
||||
] {
|
||||
if len > 1 {
|
||||
for mut oplist in get_ops_with_concat(len - 1) {
|
||||
oplist.insert(0, i);
|
||||
|
@ -146,32 +134,56 @@ mod test {
|
|||
for c in [
|
||||
(
|
||||
190,
|
||||
Vec::from([10, 19]),
|
||||
Vec::from([
|
||||
10, 19,
|
||||
]),
|
||||
Vec::from([Operation::Multiply]),
|
||||
true,
|
||||
),
|
||||
(
|
||||
3267,
|
||||
Vec::from([81, 40, 27]),
|
||||
Vec::from([Operation::Add, Operation::Multiply]),
|
||||
Vec::from([
|
||||
81, 40, 27,
|
||||
]),
|
||||
Vec::from([
|
||||
Operation::Add,
|
||||
Operation::Multiply,
|
||||
]),
|
||||
true,
|
||||
),
|
||||
(
|
||||
3267,
|
||||
Vec::from([81, 40, 27]),
|
||||
Vec::from([Operation::Multiply, Operation::Add]),
|
||||
Vec::from([
|
||||
81, 40, 27,
|
||||
]),
|
||||
Vec::from([
|
||||
Operation::Multiply,
|
||||
Operation::Add,
|
||||
]),
|
||||
true,
|
||||
),
|
||||
(
|
||||
292,
|
||||
Vec::from([11, 6, 16, 20]),
|
||||
Vec::from([Operation::Add, Operation::Multiply, Operation::Add]),
|
||||
Vec::from([
|
||||
11, 6, 16, 20,
|
||||
]),
|
||||
Vec::from([
|
||||
Operation::Add,
|
||||
Operation::Multiply,
|
||||
Operation::Add,
|
||||
]),
|
||||
true,
|
||||
),
|
||||
(
|
||||
292,
|
||||
Vec::from([11, 6, 16, 20]),
|
||||
Vec::from([Operation::Add, Operation::Multiply, Operation::Multiply]),
|
||||
Vec::from([
|
||||
11, 6, 16, 20,
|
||||
]),
|
||||
Vec::from([
|
||||
Operation::Add,
|
||||
Operation::Multiply,
|
||||
Operation::Multiply,
|
||||
]),
|
||||
false,
|
||||
),
|
||||
] {
|
||||
|
|
|
@ -136,14 +136,8 @@ fn main() {
|
|||
let mut file_disk = raw_disk.clone();
|
||||
compact_blocks(&mut block_disk);
|
||||
let mut cksum = calculate_checksum(block_disk);
|
||||
println!(
|
||||
"Checksum for the blockcompacted disk is: {}",
|
||||
cksum
|
||||
);
|
||||
println!("Checksum for the blockcompacted disk is: {}", cksum);
|
||||
compact_files(&mut file_disk);
|
||||
cksum = calculate_checksum(file_disk);
|
||||
println!(
|
||||
"Checksum for the filecompacted disk is: {}",
|
||||
cksum,
|
||||
);
|
||||
println!("Checksum for the filecompacted disk is: {}", cksum,);
|
||||
}
|
||||
|
|
|
@ -352,12 +352,12 @@ impl KartesianDirection {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn iter(none: bool, diagonal: bool, nondiagonal: bool) -> KartesianIterator {
|
||||
pub fn iter(none: bool, diagonal: bool, straight: bool) -> KartesianIterator {
|
||||
KartesianIterator {
|
||||
i: 0,
|
||||
none: none,
|
||||
diagonal: diagonal,
|
||||
nondiagonal: nondiagonal,
|
||||
none,
|
||||
diagonal,
|
||||
straight,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -383,7 +383,7 @@ pub struct KartesianIterator {
|
|||
i: u8,
|
||||
diagonal: bool,
|
||||
none: bool,
|
||||
nondiagonal: bool,
|
||||
straight: bool,
|
||||
}
|
||||
|
||||
impl Iterator for KartesianIterator {
|
||||
|
@ -391,7 +391,7 @@ impl Iterator for KartesianIterator {
|
|||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let mut i = self.i;
|
||||
if self.nondiagonal {
|
||||
if self.straight {
|
||||
if i < 4 {
|
||||
self.i += 1;
|
||||
}
|
||||
|
|
Laden …
Tabelle hinzufügen
In neuem Issue referenzieren