Skip to content

Commit

Permalink
Apply Clippy's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoholmes committed Dec 3, 2024
1 parent 8301955 commit 2e74d3a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion 2024/src/common/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ macro_rules! run_day {
let input_file = format!("inputs/input_{}.txt", $day_num);
let mut input_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
input_path.push(input_file);
let raw_input = crate::try_get_ok!(fs::read_to_string(input_path));
let raw_input = $crate::try_get_ok!(fs::read_to_string(input_path));

let start_time = Instant::now();

Expand Down
2 changes: 1 addition & 1 deletion 2024/src/common/maths.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::ops::{Mul, Div, Rem};

#[allow(dead_code)]

pub fn lcm<T>(a: T, b: T) -> T where T: Copy + Mul<Output=T> + PartialEq + Div<Output=T> + Rem<Output=T> + Default {
a * b / gcd(a, b)
}

#[allow(dead_code)]
pub fn gcd<T>(a: T, b: T) -> T where T: Copy + PartialEq + Rem<Output=T> + Default {
if b == T::default() {
a
Expand Down
2 changes: 1 addition & 1 deletion 2024/src/common/times_taken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl TimesTaken {
part_two: Duration::ZERO,
};

pub fn divide(self: &Self, divisor: u32) -> TimesTaken {
pub fn divide(&self, divisor: u32) -> TimesTaken {
TimesTaken {
total: self.total / divisor,
parsing: self.parsing / divisor,
Expand Down
2 changes: 1 addition & 1 deletion 2024/src/common/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn get_all_day_nums() -> Result<Vec<u8>, String> {
Some(filename) => extract_day_num(filename),
None => None,
},
Err(_) => return None,
Err(_) => None,
})
.collect();

Expand Down
38 changes: 21 additions & 17 deletions 2024/src/solutions/day_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,29 @@ pub fn part_two(cols: &(Vec<u32>, Vec<u32>)) -> Result<usize, String> {
let mut r = left.next();
let mut total = 0;
while l.is_some() && r.is_some() {
if l < r {
let rx = r.unwrap();
let _ = left.by_ref().skip_while(|&x| x < rx);
l = left.next();
}
else if r < l {
let lx = l.unwrap();
let _ = right.by_ref().skip_while(|&x| x < lx);
r = right.next();
}
else {
let n = l.unwrap();
let lx = *l.unwrap();
let rx = *r.unwrap();
match lx.cmp(&rx) {
std::cmp::Ordering::Less => {
let rx = r.unwrap();
let _ = left.by_ref().skip_while(|&x| x < rx);
l = left.next();
},
std::cmp::Ordering::Greater => {
let lx = l.unwrap();
let _ = right.by_ref().skip_while(|&x| x < lx);
r = right.next();
},
std::cmp::Ordering::Equal => {
let n = l.unwrap();

let l_count = 1 + left.by_ref().take_while(|&x| x == n).count(); // this seems to only ever be 1 but whatever
let r_count = 1 + right.by_ref().take_while(|&x| x == n).count();
total += l_count * r_count;
let l_count = 1 + left.by_ref().take_while(|&x| x == n).count(); // this seems to only ever be 1 but whatever
let r_count = 1 + right.by_ref().take_while(|&x| x == n).count();
total += l_count * r_count;

l = left.next();
r = right.next();
l = left.next();
r = right.next();
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion 2024/src/solutions/day_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn check_safe(line: &[i32]) -> bool {
pprev = prev;
prev = *x;
}
return true;
true
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions 2024/src/solutions/day_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn parse(input_raw: &str) -> Result<Vec<Parsed>, String> {
let mut tokenized = vec![];
let mut start = 0;
while start < input_raw.len() {
if let Some(idx) = input_raw[start..].find(&['d', 'm']) {
if let Some(idx) = input_raw[start..].find(['d', 'm']) {
start += idx;
} else {
break;
Expand All @@ -27,7 +27,7 @@ pub fn parse(input_raw: &str) -> Result<Vec<Parsed>, String> {
}
else if input_raw[start..].starts_with("mul(") {
start += 4;
let Some(idx) = input_raw[start..].find(|c| c == ')') else {
let Some(idx) = input_raw[start..].find(')') else {
continue;
};

Expand Down

0 comments on commit 2e74d3a

Please sign in to comment.