2019: fix new clippy lints

This commit is contained in:
Antoine Martin 2020-12-14 19:45:51 +01:00
parent deb808faf3
commit 9a2e37468f
5 changed files with 83 additions and 60 deletions

View file

@ -190,7 +190,7 @@ impl FromStr for Move {
fn from_str(s: &str) -> Result<Self> { fn from_str(s: &str) -> Result<Self> {
let direction = s let direction = s
.chars() .chars()
.nth(0) .next()
.context("couldn't get direction char in move")?; .context("couldn't get direction char in move")?;
let s = s.get(1..).context("move missing length")?; let s = s.get(1..).context("move missing length")?;

View file

@ -1,3 +1,4 @@
use std::cmp::Ordering;
use std::fmt::Write; use std::fmt::Write;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
@ -59,17 +60,21 @@ fn part2(begin: usize, end: usize) -> Result<usize> {
let mut count = 1; let mut count = 1;
let mut prev = digits[0]; let mut prev = digits[0];
for digit in digits.iter().skip(1) { for digit in digits.iter().skip(1) {
if prev > *digit { match prev.cmp(digit) {
Ordering::Greater => {
ordered = false; ordered = false;
break; break;
} else if prev == *digit { }
Ordering::Equal => {
count += 1; count += 1;
} else { }
Ordering::Less => {
if count == 2 { if count == 2 {
pair = true; pair = true;
} }
count = 1; count = 1;
} }
}
prev = *digit; prev = *digit;
} }

View file

@ -1,3 +1,4 @@
use std::cmp::Ordering;
use std::fmt::Write; use std::fmt::Write;
use std::str::FromStr; use std::str::FromStr;
@ -52,29 +53,41 @@ fn part1(mut planets: Vec<Planet>, steps: usize) -> Result<u64> {
let first = &mut begin[pair.0]; let first = &mut begin[pair.0];
let second = &mut end[0]; let second = &mut end[0];
if first.position.x > second.position.x { match first.position.x.cmp(&second.position.x) {
Ordering::Greater => {
first.velocity.x -= 1; first.velocity.x -= 1;
second.velocity.x += 1; second.velocity.x += 1;
} else if first.position.x < second.position.x { }
Ordering::Less => {
first.velocity.x += 1; first.velocity.x += 1;
second.velocity.x -= 1; second.velocity.x -= 1;
} }
Ordering::Equal => {}
}
if first.position.y > second.position.y { match first.position.y.cmp(&second.position.y) {
Ordering::Greater => {
first.velocity.y -= 1; first.velocity.y -= 1;
second.velocity.y += 1; second.velocity.y += 1;
} else if first.position.y < second.position.y { }
Ordering::Less => {
first.velocity.y += 1; first.velocity.y += 1;
second.velocity.y -= 1; second.velocity.y -= 1;
} }
Ordering::Equal => {}
}
if first.position.z > second.position.z { match first.position.z.cmp(&second.position.z) {
Ordering::Greater => {
first.velocity.z -= 1; first.velocity.z -= 1;
second.velocity.z += 1; second.velocity.z += 1;
} else if first.position.z < second.position.z { }
Ordering::Less => {
first.velocity.z += 1; first.velocity.z += 1;
second.velocity.z -= 1; second.velocity.z -= 1;
} }
Ordering::Equal => {}
}
} }
// update position // update position
@ -114,29 +127,41 @@ fn part2(mut planets: Vec<Planet>) -> Result<usize> {
let first = &mut begin[pair.0]; let first = &mut begin[pair.0];
let second = &mut end[0]; let second = &mut end[0];
if first.position.x > second.position.x { match first.position.x.cmp(&second.position.x) {
Ordering::Greater => {
first.velocity.x -= 1; first.velocity.x -= 1;
second.velocity.x += 1; second.velocity.x += 1;
} else if first.position.x < second.position.x { }
Ordering::Less => {
first.velocity.x += 1; first.velocity.x += 1;
second.velocity.x -= 1; second.velocity.x -= 1;
} }
Ordering::Equal => {}
}
if first.position.y > second.position.y { match first.position.y.cmp(&second.position.y) {
Ordering::Greater => {
first.velocity.y -= 1; first.velocity.y -= 1;
second.velocity.y += 1; second.velocity.y += 1;
} else if first.position.y < second.position.y { }
Ordering::Less => {
first.velocity.y += 1; first.velocity.y += 1;
second.velocity.y -= 1; second.velocity.y -= 1;
} }
Ordering::Equal => {}
}
if first.position.z > second.position.z { match first.position.z.cmp(&second.position.z) {
Ordering::Greater => {
first.velocity.z -= 1; first.velocity.z -= 1;
second.velocity.z += 1; second.velocity.z += 1;
} else if first.position.z < second.position.z { }
Ordering::Less => {
first.velocity.z += 1; first.velocity.z += 1;
second.velocity.z -= 1; second.velocity.z -= 1;
} }
Ordering::Equal => {}
}
} }
// update position // update position

View file

@ -1,3 +1,4 @@
use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{self, Display, Write}; use std::fmt::{self, Display, Write};
@ -30,13 +31,7 @@ fn part1(memory: Vec<i64>) -> Result<usize> {
map.insert(pos, tile); map.insert(pos, tile);
} }
Ok(map Ok(map.values().filter(|t| matches!(t, Tile::Block)).count())
.values()
.filter(|t| match t {
Tile::Block => true,
_ => false,
})
.count())
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -50,12 +45,10 @@ fn print_screen(screen: &[Vec<Tile>]) {
} }
fn get_next_move(paddle_pos: (i64, i64), ball_pos: (i64, i64)) -> i64 { fn get_next_move(paddle_pos: (i64, i64), ball_pos: (i64, i64)) -> i64 {
if ball_pos.0 > paddle_pos.0 { match ball_pos.0.cmp(&paddle_pos.0) {
1 Ordering::Greater => 1,
} else if ball_pos.0 == paddle_pos.0 { Ordering::Equal => 0,
0 Ordering::Less => -1,
} else {
-1
} }
} }

View file

@ -90,7 +90,7 @@ fn get_ore_cost(
in_stock += num_reactions * recipe.produced; in_stock += num_reactions * recipe.produced;
} }
inventory.insert(material.clone(), in_stock - quantity); inventory.insert(material, in_stock - quantity);
Ok(total) Ok(total)
} }