From d6f16f89a5c3ed2eec10c3966903944bcb1b6b81 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 2 Dec 2023 08:23:05 +0100 Subject: [PATCH 1/2] replaced map().sum() with fold --- 2023/rust/day-01/src/part2.rs | 18 ++++------------ 2023/rust/day-01/src/part2_aho_corasick.rs | 24 ++++++---------------- 2023/rust/day-01/src/part2_nom.rs | 12 +++-------- 3 files changed, 13 insertions(+), 41 deletions(-) diff --git a/2023/rust/day-01/src/part2.rs b/2023/rust/day-01/src/part2.rs index abf65ae..bdfda4d 100644 --- a/2023/rust/day-01/src/part2.rs +++ b/2023/rust/day-01/src/part2.rs @@ -1,11 +1,8 @@ use crate::custom_error::AocError; #[tracing::instrument] -pub fn process( - input: &str, -) -> miette::Result { - let output = - input.lines().map(process_line).sum::(); +pub fn process(input: &str) -> miette::Result { + let output = input.lines().fold(0, |acc, line| acc + process_line(line)); Ok(output.to_string()) } @@ -33,11 +30,7 @@ fn process_line(line: &str) -> u32 { } else if reduced_line.starts_with("nine") { Some(9) } else { - reduced_line - .chars() - .next() - .unwrap() - .to_digit(10) + reduced_line.chars().next().unwrap().to_digit(10) }; result @@ -68,10 +61,7 @@ mod tests { /// it tests two overlapping numbers /// where the second number should succeed #[case("fivezg8jmf6hrxnhgxxttwoneg", 51)] - fn line_test( - #[case] line: &str, - #[case] expected: u32, - ) { + fn line_test(#[case] line: &str, #[case] expected: u32) { assert_eq!(expected, process_line(line)) } diff --git a/2023/rust/day-01/src/part2_aho_corasick.rs b/2023/rust/day-01/src/part2_aho_corasick.rs index 7be853a..fe7b23c 100644 --- a/2023/rust/day-01/src/part2_aho_corasick.rs +++ b/2023/rust/day-01/src/part2_aho_corasick.rs @@ -2,19 +2,15 @@ use crate::custom_error::AocError; use aho_corasick::AhoCorasick; #[tracing::instrument] -pub fn process( - input: &str, -) -> miette::Result { - let output = - input.lines().map(process_line).sum::(); +pub fn process(input: &str) -> miette::Result { + let output = input.lines().fold(0, |acc, line| acc + process_line(line)); Ok(output.to_string()) } const PATTERNS: [&str; 19] = [ - "one", "two", "three", "four", "five", "six", "seven", - "eight", "nine", "0", "1", "2", "3", "4", "5", "6", - "7", "8", "9", + "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "0", "1", "2", "3", + "4", "5", "6", "7", "8", "9", ]; #[tracing::instrument] @@ -22,12 +18,7 @@ fn process_line(line: &str) -> u32 { let ac = AhoCorasick::new(PATTERNS).unwrap(); let mut it = ac.find_overlapping_iter(line); - let first = from_matchables( - PATTERNS[it - .next() - .expect("should be a number") - .pattern()], - ); + let first = from_matchables(PATTERNS[it.next().expect("should be a number").pattern()]); match it .last() @@ -82,10 +73,7 @@ mod tests { /// it tests two overlapping numbers /// where the second number should succeed #[case("fivezg8jmf6hrxnhgxxttwoneg", 51)] - fn line_test( - #[case] line: &str, - #[case] expected: u32, - ) { + fn line_test(#[case] line: &str, #[case] expected: u32) { assert_eq!(expected, process_line(line)) } diff --git a/2023/rust/day-01/src/part2_nom.rs b/2023/rust/day-01/src/part2_nom.rs index 1013880..24c7b42 100644 --- a/2023/rust/day-01/src/part2_nom.rs +++ b/2023/rust/day-01/src/part2_nom.rs @@ -10,11 +10,8 @@ use nom::{ use crate::custom_error::AocError; #[tracing::instrument] -pub fn process( - input: &str, -) -> miette::Result { - let output = - input.lines().map(process_line).sum::(); +pub fn process(input: &str) -> miette::Result { + let output = input.lines().fold(0, |acc, line| acc + process_line(line)); Ok(output.to_string()) } @@ -83,10 +80,7 @@ mod tests { /// it tests two overlapping numbers /// where the second number should succeed #[case("fivezg8jmf6hrxnhgxxttwoneg", 51)] - fn line_test( - #[case] line: &str, - #[case] expected: u32, - ) { + fn line_test(#[case] line: &str, #[case] expected: u32) { assert_eq!(expected, process_line(line)) } From 002c2ce2bbb5c0302e5ce5e9e4ae2713a3557b2d Mon Sep 17 00:00:00 2001 From: Christopher Biscardi Date: Fri, 8 Dec 2023 16:38:00 -0800 Subject: [PATCH 2/2] fmt --- 2023/rust/day-01/src/part2.rs | 19 ++++++++++++---- 2023/rust/day-01/src/part2_aho_corasick.rs | 25 ++++++++++++++++------ 2023/rust/day-01/src/part2_nom.rs | 13 ++++++++--- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/2023/rust/day-01/src/part2.rs b/2023/rust/day-01/src/part2.rs index bdfda4d..ad98280 100644 --- a/2023/rust/day-01/src/part2.rs +++ b/2023/rust/day-01/src/part2.rs @@ -1,8 +1,12 @@ use crate::custom_error::AocError; #[tracing::instrument] -pub fn process(input: &str) -> miette::Result { - let output = input.lines().fold(0, |acc, line| acc + process_line(line)); +pub fn process( + input: &str, +) -> miette::Result { + let output = input + .lines() + .fold(0, |acc, line| acc + process_line(line)); Ok(output.to_string()) } @@ -30,7 +34,11 @@ fn process_line(line: &str) -> u32 { } else if reduced_line.starts_with("nine") { Some(9) } else { - reduced_line.chars().next().unwrap().to_digit(10) + reduced_line + .chars() + .next() + .unwrap() + .to_digit(10) }; result @@ -61,7 +69,10 @@ mod tests { /// it tests two overlapping numbers /// where the second number should succeed #[case("fivezg8jmf6hrxnhgxxttwoneg", 51)] - fn line_test(#[case] line: &str, #[case] expected: u32) { + fn line_test( + #[case] line: &str, + #[case] expected: u32, + ) { assert_eq!(expected, process_line(line)) } diff --git a/2023/rust/day-01/src/part2_aho_corasick.rs b/2023/rust/day-01/src/part2_aho_corasick.rs index fe7b23c..a3c7e19 100644 --- a/2023/rust/day-01/src/part2_aho_corasick.rs +++ b/2023/rust/day-01/src/part2_aho_corasick.rs @@ -2,15 +2,20 @@ use crate::custom_error::AocError; use aho_corasick::AhoCorasick; #[tracing::instrument] -pub fn process(input: &str) -> miette::Result { - let output = input.lines().fold(0, |acc, line| acc + process_line(line)); +pub fn process( + input: &str, +) -> miette::Result { + let output = input + .lines() + .fold(0, |acc, line| acc + process_line(line)); Ok(output.to_string()) } const PATTERNS: [&str; 19] = [ - "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "0", "1", "2", "3", - "4", "5", "6", "7", "8", "9", + "one", "two", "three", "four", "five", "six", "seven", + "eight", "nine", "0", "1", "2", "3", "4", "5", "6", + "7", "8", "9", ]; #[tracing::instrument] @@ -18,7 +23,12 @@ fn process_line(line: &str) -> u32 { let ac = AhoCorasick::new(PATTERNS).unwrap(); let mut it = ac.find_overlapping_iter(line); - let first = from_matchables(PATTERNS[it.next().expect("should be a number").pattern()]); + let first = from_matchables( + PATTERNS[it + .next() + .expect("should be a number") + .pattern()], + ); match it .last() @@ -73,7 +83,10 @@ mod tests { /// it tests two overlapping numbers /// where the second number should succeed #[case("fivezg8jmf6hrxnhgxxttwoneg", 51)] - fn line_test(#[case] line: &str, #[case] expected: u32) { + fn line_test( + #[case] line: &str, + #[case] expected: u32, + ) { assert_eq!(expected, process_line(line)) } diff --git a/2023/rust/day-01/src/part2_nom.rs b/2023/rust/day-01/src/part2_nom.rs index 24c7b42..3bd6ff9 100644 --- a/2023/rust/day-01/src/part2_nom.rs +++ b/2023/rust/day-01/src/part2_nom.rs @@ -10,8 +10,12 @@ use nom::{ use crate::custom_error::AocError; #[tracing::instrument] -pub fn process(input: &str) -> miette::Result { - let output = input.lines().fold(0, |acc, line| acc + process_line(line)); +pub fn process( + input: &str, +) -> miette::Result { + let output = input + .lines() + .fold(0, |acc, line| acc + process_line(line)); Ok(output.to_string()) } @@ -80,7 +84,10 @@ mod tests { /// it tests two overlapping numbers /// where the second number should succeed #[case("fivezg8jmf6hrxnhgxxttwoneg", 51)] - fn line_test(#[case] line: &str, #[case] expected: u32) { + fn line_test( + #[case] line: &str, + #[case] expected: u32, + ) { assert_eq!(expected, process_line(line)) }