From 45f780c1ca134bb9d2b4c1fdfb82e29a5a9622b6 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sun, 5 Dec 2021 14:51:58 -0800 Subject: [PATCH] Address `cargo clippy` output. --- 2021/src/day1.rs | 2 +- 2021/src/day2.rs | 8 ++++---- 2021/src/day3.rs | 22 ++++++++-------------- 2021/src/day4.rs | 14 +++++--------- 2021/src/day5.rs | 6 +++--- 2021/src/main.rs | 2 -- 6 files changed, 21 insertions(+), 33 deletions(-) diff --git a/2021/src/day1.rs b/2021/src/day1.rs index 4962ecb..253b834 100644 --- a/2021/src/day1.rs +++ b/2021/src/day1.rs @@ -79,7 +79,7 @@ use aoc_runner_derive::{aoc, aoc_generator}; /// Reads text file containing one integer per line, and parses them into `Vec`. #[aoc_generator(day1)] fn parse(input: &str) -> Result> { - input.split("\n").map(|s| Ok(s.parse()?)).collect() + input.split('\n').map(|s| Ok(s.parse()?)).collect() } #[aoc(day1, part1)] diff --git a/2021/src/day2.rs b/2021/src/day2.rs index 756009a..130b4bd 100644 --- a/2021/src/day2.rs +++ b/2021/src/day2.rs @@ -59,8 +59,8 @@ use aoc_runner_derive::aoc; fn part1(input: &str) -> Result { let mut horizontal: i32 = 0; let mut depth: i32 = 0; - for l in input.split("\n") { - let p: Vec<_> = l.split(" ").collect(); + for l in input.split('\n') { + let p: Vec<_> = l.split(' ').collect(); match p[0] { "forward" => horizontal += p[1].parse::()?, @@ -77,8 +77,8 @@ fn part2(input: &str) -> Result { let mut horizontal: i32 = 0; let mut depth: i32 = 0; let mut aim: i32 = 0; - for l in input.split("\n") { - let p: Vec<_> = l.split(" ").collect(); + for l in input.split('\n') { + let p: Vec<_> = l.split(' ').collect(); match p[0] { "forward" => { diff --git a/2021/src/day3.rs b/2021/src/day3.rs index c8d03a7..411c207 100644 --- a/2021/src/day3.rs +++ b/2021/src/day3.rs @@ -61,16 +61,14 @@ //! //! Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together. What is the life support rating of the submarine? (Be sure to represent your answer in decimal, not binary.) -use std::fmt::Debug; -use std::fmt::Error; -use std::fmt::Formatter; +use std::fmt::{Debug, Error, Formatter}; use anyhow::Result; use aoc_runner_derive::aoc; #[aoc(day3, part1)] fn part1(input: &str) -> Result { - let lines: Vec<_> = input.trim().split("\n").collect(); + let lines: Vec<_> = input.trim().split('\n').collect(); let num_bits = lines[0].len(); let majority = lines.len() / 2; let mut bits = vec![0; num_bits]; @@ -109,11 +107,11 @@ enum Partition { struct Binaries<'a>(&'a [u64]); impl<'a> Debug for Binaries<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { - write!(f, "[\n")?; + writeln!(f, "[")?; for n in self.0.iter() { - write!(f, " 0b{:08b},\n", n)?; + writeln!(f, " 0b{:08b},", n)?; } - write!(f, "]\n")?; + writeln!(f, "]")?; Ok(()) } } @@ -124,18 +122,14 @@ fn partition(nums: &[u64], bit_offset: usize, partition_type: Partition) -> u64 let remainder = match partition_type { Partition::Oxygen => { - if one.len() == zero.len() { - one - } else if one.len() > zero.len() { + if one.len() >= zero.len() { one } else { zero } } Partition::CO2 => { - if one.len() == zero.len() { - zero - } else if one.len() > zero.len() { + if one.len() >= zero.len() { zero } else { one @@ -150,7 +144,7 @@ fn partition(nums: &[u64], bit_offset: usize, partition_type: Partition) -> u64 #[aoc(day3, part2)] fn part2(input: &str) -> Result { - let lines: Vec<_> = input.trim().split("\n").collect(); + let lines: Vec<_> = input.trim().split('\n').collect(); let nums: Vec<_> = lines .iter() .map(|s| u64::from_str_radix(s, 2)) diff --git a/2021/src/day4.rs b/2021/src/day4.rs index 49b47f6..26e56dc 100644 --- a/2021/src/day4.rs +++ b/2021/src/day4.rs @@ -87,8 +87,6 @@ enum GameError { ParseIntError(#[from] ParseIntError), #[error("couldn't parse board {0}")] BoardError(#[from] BoardError), - #[error("unknown")] - Unknown, } impl Game { @@ -130,7 +128,7 @@ impl FromStr for Game { let numbers = it .next() .unwrap() - .split(",") + .split(',') .map(|s| s.parse()) .collect::>()?; let boards: Vec<_> = it.map(|s| s.parse()).collect::>()?; @@ -152,8 +150,6 @@ struct Board { enum BoardError { #[error("couldn't parse number {0}")] ParseIntError(#[from] ParseIntError), - #[error("unknown")] - Unknown, } impl Board { @@ -206,7 +202,7 @@ impl Debug for Board { write!(f, "{:3}", self.numbers[&(x, y)])?; } } - writeln!(f, "")?; + writeln!(f)?; } Ok(()) } @@ -217,11 +213,11 @@ impl FromStr for Board { fn from_str(s: &str) -> Result { let numbers: Vec> = s - .split("\n") + .split('\n') .map(|l| { - l.split(" ") + l.split(' ') // Remove the double space that happens before single digit cells. - .filter(|c| *c != "") + .filter(|c| !c.is_empty()) .map(|c| c.parse()) .collect::>() }) diff --git a/2021/src/day5.rs b/2021/src/day5.rs index 8ca5f88..a54d4ab 100644 --- a/2021/src/day5.rs +++ b/2021/src/day5.rs @@ -105,11 +105,11 @@ impl FromStr for Line { type Err = LineError; fn from_str(s: &str) -> Result { - let mut it = s.split(" "); + let mut it = s.split(' '); let parse_point = |it: &mut dyn Iterator| -> Result { let p = it.next().ok_or(LineError::PrematureEOL)?; let nums: Vec<_> = p - .split(",") + .split(',') .map(|n| n.parse()) .collect::>()?; Ok(Point { @@ -178,7 +178,7 @@ impl Debug for Image { #[aoc_generator(day5)] fn parse(input: &str) -> Result> { Ok(input - .split("\n") + .split('\n') .map(|l| l.parse()) .collect::>()?) } diff --git a/2021/src/main.rs b/2021/src/main.rs index c2829c7..5f25329 100644 --- a/2021/src/main.rs +++ b/2021/src/main.rs @@ -1,5 +1,3 @@ -use advent2021; - use aoc_runner_derive::aoc_main; aoc_main! { lib = advent2021 }