Address cargo clippy output.

This commit is contained in:
Bill Thiede 2021-12-05 14:51:58 -08:00
parent 5b65e8ec71
commit 45f780c1ca
6 changed files with 21 additions and 33 deletions

View File

@ -79,7 +79,7 @@ use aoc_runner_derive::{aoc, aoc_generator};
/// Reads text file containing one integer per line, and parses them into `Vec<u32>`.
#[aoc_generator(day1)]
fn parse(input: &str) -> Result<Vec<u32>> {
input.split("\n").map(|s| Ok(s.parse()?)).collect()
input.split('\n').map(|s| Ok(s.parse()?)).collect()
}
#[aoc(day1, part1)]

View File

@ -59,8 +59,8 @@ use aoc_runner_derive::aoc;
fn part1(input: &str) -> Result<i32> {
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::<i32>()?,
@ -77,8 +77,8 @@ fn part2(input: &str) -> Result<i32> {
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" => {

View File

@ -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<u64> {
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<u64> {
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))

View File

@ -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::<Result<_, ParseIntError>>()?;
let boards: Vec<_> = it.map(|s| s.parse()).collect::<Result<_, BoardError>>()?;
@ -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<Self, Self::Err> {
let numbers: Vec<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::<Result<_, ParseIntError>>()
})

View File

@ -105,11 +105,11 @@ impl FromStr for Line {
type Err = LineError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut it = s.split(" ");
let mut it = s.split(' ');
let parse_point = |it: &mut dyn Iterator<Item = &str>| -> Result<Point, LineError> {
let p = it.next().ok_or(LineError::PrematureEOL)?;
let nums: Vec<_> = p
.split(",")
.split(',')
.map(|n| n.parse())
.collect::<Result<_, ParseIntError>>()?;
Ok(Point {
@ -178,7 +178,7 @@ impl Debug for Image {
#[aoc_generator(day5)]
fn parse(input: &str) -> Result<Vec<Line>> {
Ok(input
.split("\n")
.split('\n')
.map(|l| l.parse())
.collect::<Result<_, LineError>>()?)
}

View File

@ -1,5 +1,3 @@
use advent2021;
use aoc_runner_derive::aoc_main;
aoc_main! { lib = advent2021 }