From a27587f18fb0891135e6996741d91421b592b527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=A9=20Cassiop=C3=A9e=20Gauthier?= Date: Tue, 14 Dec 2021 17:20:24 -0500 Subject: [PATCH] Remove usage of std::io::Cursor --- src/day1.rs | 19 +++++++------------ src/day12.rs | 2 +- src/day2.rs | 25 ++++++++++--------------- src/day3.rs | 4 +--- 4 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/day1.rs b/src/day1.rs index fc20e33..ac9afbf 100644 --- a/src/day1.rs +++ b/src/day1.rs @@ -1,9 +1,5 @@ -use std::io::{self, BufRead}; - -fn increases(measurements: &[u64]) -> u64 { - measurements - .windows(2) - .fold(0, |acc, x| if x[1] > x[0] { acc + 1 } else { acc }) +fn increases(measurements: &[u64]) -> usize { + measurements.windows(2).filter(|x| x[1] > x[0]).count() } fn convolve(measurements: &[u64]) -> Vec { @@ -12,23 +8,22 @@ fn convolve(measurements: &[u64]) -> Vec { fn main() { const INPUT: &str = include_str!("../inputs/day1.txt"); - let measurements: Vec = io::Cursor::new(INPUT) + let measurements = INPUT .lines() - .map(|line| line.unwrap().parse().unwrap()) - .collect(); - + .map(|line| line.parse().unwrap()) + .collect::>(); println!("increased {} times", increases(&measurements)); println!("increased {} times", increases(&convolve(&measurements))); } #[test] fn part1_example() { - let measurements = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263]; + let measurements = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263]; assert_eq!(increases(&measurements), 7); } #[test] fn part2_example() { - let measurements = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263]; + let measurements = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263]; assert_eq!(increases(&convolve(&measurements)), 5); } diff --git a/src/day12.rs b/src/day12.rs index 254f011..32f386e 100644 --- a/src/day12.rs +++ b/src/day12.rs @@ -33,7 +33,7 @@ fn is_small_cave(cave: &str) -> bool { if cave == "start" { false } else { - cave.chars().all(|c| matches!(c, 'a'..='z')) + cave.chars().all(|c| c.is_ascii_lowercase()) } } diff --git a/src/day2.rs b/src/day2.rs index 5108e13..51ab8fb 100644 --- a/src/day2.rs +++ b/src/day2.rs @@ -1,5 +1,3 @@ -use std::io::{self, BufRead}; -use std::num::ParseIntError; use std::str::FromStr; enum Action { @@ -14,30 +12,27 @@ struct Command { } impl FromStr for Command { - type Err = ParseIntError; + type Err = (); fn from_str(s: &str) -> Result { let words = s.split_once(' ').unwrap(); let action = match words.0 { - "forward" => Ok(Action::Forward), - "down" => Ok(Action::Down), - "up" => Ok(Action::Up), - _ => Err(()), + "forward" => Action::Forward, + "down" => Action::Down, + "up" => Action::Up, + _ => return Err(()), }; - let units = words.1.parse()?; - Ok(Command { - action: action.unwrap(), - units, - }) + let units = words.1.parse().unwrap(); + Ok(Command { action, units }) } } fn main() { const INPUT: &str = include_str!("../inputs/day2.txt"); - let commands: Vec = io::Cursor::new(INPUT) + let commands = INPUT .lines() - .map(|l| l.unwrap().parse().unwrap()) - .collect(); + .map(|line| line.parse().unwrap()) + .collect::>(); let mut position = 0; let mut depth = 0; diff --git a/src/day3.rs b/src/day3.rs index a3281a1..71c1cf9 100644 --- a/src/day3.rs +++ b/src/day3.rs @@ -1,5 +1,3 @@ -use std::io::{self, BufRead}; - struct Diagnostics { report: Vec, } @@ -88,7 +86,7 @@ impl Diagnostics { fn main() { const INPUT: &str = include_str!("../inputs/day3.txt"); - let report: Vec = io::Cursor::new(INPUT).lines().map(|l| l.unwrap()).collect(); + let report = INPUT.lines().map(|line| line.to_string()).collect::>(); let diagnostics = Diagnostics::<12> { report }; println!(