From b5c89d906f2614146eb5d195c527fdf67b9e9cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Kucha=C5=99?= Date: Tue, 3 Dec 2024 08:55:47 +0100 Subject: [PATCH] refactor: cleanup all days --- data/examples/03.txt | 1 - src/bin/01.rs | 4 ++-- src/bin/02.rs | 13 ++++++------- src/bin/03.rs | 3 ++- 4 files changed, 10 insertions(+), 11 deletions(-) delete mode 100644 data/examples/03.txt diff --git a/data/examples/03.txt b/data/examples/03.txt deleted file mode 100644 index f274bda..0000000 --- a/data/examples/03.txt +++ /dev/null @@ -1 +0,0 @@ -xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) diff --git a/src/bin/01.rs b/src/bin/01.rs index 2aadd43..fe305f4 100644 --- a/src/bin/01.rs +++ b/src/bin/01.rs @@ -7,8 +7,8 @@ fn get_lists(input: &str) -> (Vec, Vec) { .lines() .flat_map(|line| line.split_whitespace().map(|x| x.parse::().unwrap())) .collect(); - let left = numbers.iter().step_by(2).map(|x| *x).collect(); - let right = numbers.iter().skip(1).map(|x| *x).step_by(2).collect(); + let left = numbers.iter().step_by(2).copied().collect(); + let right = numbers.iter().skip(1).step_by(2).copied().collect(); (left, right) } diff --git a/src/bin/02.rs b/src/bin/02.rs index 6c887f0..14c715e 100644 --- a/src/bin/02.rs +++ b/src/bin/02.rs @@ -6,20 +6,19 @@ fn is_ok(sequence: &[i32]) -> bool { sequence .windows(2) .map(|x| x[0] - x[1]) - .fold(Some(Ordering::Equal), |acc, x| { + .try_fold(Ordering::Equal, |acc, x| { let ord = x.cmp(&0); if x.abs() >= 1 && x.abs() <= 3 { match acc { - Some(Ordering::Equal) => Some(ord), - Some(Ordering::Less) => match ord { + Ordering::Equal => Some(ord), + Ordering::Less => match ord { Ordering::Less => Some(Ordering::Less), _ => None, }, - Some(Ordering::Greater) => match ord { + Ordering::Greater => match ord { Ordering::Greater => Some(Ordering::Greater), _ => None, }, - None => None, } } else { None @@ -43,7 +42,7 @@ pub fn part_one(input: &str) -> Option { Some( parse_input(input) .iter() - .map(|line| is_ok(&line)) + .map(|line| is_ok(line)) .filter(|x| *x) .count() as u32, ) @@ -54,7 +53,7 @@ pub fn part_two(input: &str) -> Option { parse_input(input) .iter() .map(|line| { - let mut ok = is_ok(&line); + let mut ok = is_ok(line); for i in 0..line.len() { let mut line = line.clone(); line.remove(i); diff --git a/src/bin/03.rs b/src/bin/03.rs index 5822297..d0b65f7 100644 --- a/src/bin/03.rs +++ b/src/bin/03.rs @@ -55,7 +55,8 @@ mod tests { #[test] fn test_part_one() { - let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + let result = + part_one("xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"); assert_eq!(result, Some(161)); }