Fix all clippy warnings.

This commit is contained in:
2018-12-17 12:26:31 +01:00
parent 851868bed4
commit f23624c456
15 changed files with 52 additions and 38 deletions

View File

@@ -15,7 +15,7 @@ use std::fmt::Debug;
/// assumed to be filled with "true" before being handed to this
/// method.
pub fn prime_sieve(dest: &mut [bool]) {
if dest.len() >= 1 {
if !dest.is_empty() {
dest[0] = false;
}
@@ -25,7 +25,7 @@ pub fn prime_sieve(dest: &mut [bool]) {
let limit = (dest.len() as f64).sqrt() as usize;
for i in 1..(limit + 1) {
for i in 1..=limit {
if !dest[i] {
continue;
}

View File

@@ -4,11 +4,12 @@ use std::io::prelude::*;
use common;
#[derive(Default)]
pub struct Day01 {}
impl Day01 {
pub fn new() -> Day01 {
Day01 {}
Default::default()
}
}

View File

@@ -88,9 +88,9 @@ mod tests {
fn test_count_letters() {
let result = count_chars("abcaba");
assert_eq!(3, *result.get(&'a').unwrap());
assert_eq!(2, *result.get(&'b').unwrap());
assert_eq!(1, *result.get(&'c').unwrap())
assert_eq!(3, result[&'a']);
assert_eq!(2, result[&'b']);
assert_eq!(1, result[&'c']);
}
#[test]

View File

@@ -86,7 +86,7 @@ impl common::Solution for Day03 {
let claims = self.get_claims();
let uncontested = self.claims.iter()
.position(|x| x.range().all(|x| *claims.get(&x).unwrap() == 1))
.position(|x| x.range().all(|x| claims[&x] == 1))
.unwrap();
format!("{}", uncontested + 1)

View File

@@ -72,7 +72,7 @@ impl Day04 {
sleep_start = None;
}
EventType::SLEEP => {
sleep_start = Some(event.time.clone());
sleep_start = Some(event.time);
}
EventType::WAKE => {
let mut minutes = sleeps.entry(guard.unwrap()).or_insert([0u32; 60]);
@@ -86,7 +86,7 @@ impl Day04 {
sleeps
}
fn format_results(sleepers: HashMap<usize, [u32; 60]>, scores: HashMap<usize, u32>) -> String {
fn format_results(sleepers: &HashMap<usize, [u32; 60]>, scores: &HashMap<usize, u32>) -> String {
let (best_sleeper, _) = scores.iter().max_by(|&(_, a), &(_, b)| a.cmp(b)).unwrap();
let best_minute = sleepers[best_sleeper].iter().enumerate()
@@ -102,7 +102,7 @@ impl common::Solution for Day04 {
let sleepers = self.get_sleeps();
let scores: HashMap<usize, u32> = sleepers.iter().map(|(k, v)| (*k, v.iter().sum())).collect();
Day04::format_results(sleepers, scores)
Day04::format_results(&sleepers, &scores)
}
fn part2(&mut self, input: &mut io::Read) -> String {
@@ -110,7 +110,7 @@ impl common::Solution for Day04 {
let sleepers = self.get_sleeps();
let scores: HashMap<usize, u32> = sleepers.iter().map(|(k, v)| (*k, *v.iter().max().unwrap())).collect();
Day04::format_results(sleepers, scores)
Day04::format_results(&sleepers, &scores)
}
}

View File

@@ -42,8 +42,8 @@ impl common::Solution for Day05 {
input.read_to_end(&mut data).expect("Can't read input!");
common::trim_back(&mut data);
let min_len = (b'a'..=b'z').map(|option| data.iter().filter(|&x| !x.eq_ignore_ascii_case(&option)).map(|&x| x).collect())
.map(|x| Day05::reduce(x))
let min_len = (b'a'..=b'z').map(|option| data.iter().filter(|&x| !x.eq_ignore_ascii_case(&option)).cloned().collect())
.map(Day05::reduce)
.min().unwrap();
format!("{}", min_len)

View File

@@ -83,7 +83,7 @@ impl Day06 {
self.range()
.map(|x| self.points.iter().map(|y| manhattan_distance(x, *y)).sum::<usize>())
.filter(|x| x < &limit)
.filter(|&x| x < limit)
.count()
}
}
@@ -100,10 +100,11 @@ impl Solution for Day06 {
self.read_points(input);
let grid = self.compute_claim_grid();
let mut infinite: HashSet<usize> = HashSet::new();
infinite.extend(grid[0].iter().filter_map(claim_filter));
infinite.extend(grid[self.ymax].iter().filter_map(claim_filter));
for y in 0..=self.ymax {
infinite.extend([grid[y][0], grid[y][self.xmax]].iter().filter_map(claim_filter));
infinite.extend(grid.first().unwrap().iter().filter_map(claim_filter));
infinite.extend(grid.last().unwrap().iter().filter_map(claim_filter));
for row in grid.iter().take(self.ymax) {
infinite.extend([row.first().unwrap(), row.last().unwrap()].iter()
.cloned().filter_map(claim_filter));
}
let counts = grid.iter().flat_map(|x| x.iter())

View File

@@ -50,7 +50,7 @@ impl Day07 {
let a = groups[1].chars().next().unwrap();
let b = groups[2].chars().next().unwrap();
self.forward.entry(a).or_insert(Vec::new()).push(b);
self.forward.entry(a).or_insert_with(Vec::new).push(b);
*self.dep_count.entry(b).or_insert(0) += 1;
}
}

View File

@@ -61,7 +61,7 @@ impl Solution for Day08 {
fn part1(&mut self, input: &mut Read) -> String {
let data: String = read_single_input(input);
let data: Vec<usize> = data.trim().split(" ").map(|x| x.parse().unwrap()).collect();
let data: Vec<usize> = data.trim().split(' ').map(|x| x.parse().unwrap()).collect();
let (result, _) = total1(&data);
format!("{}", result)
@@ -70,7 +70,7 @@ impl Solution for Day08 {
fn part2(&mut self, input: &mut Read) -> String {
let data: String = read_single_input(input);
let data: Vec<usize> = data.trim().split(" ").map(|x| x.parse().unwrap()).collect();
let data: Vec<usize> = data.trim().split(' ').map(|x| x.parse().unwrap()).collect();
let (result, _) = total2(&data);
format!("{}", result)

View File

@@ -43,9 +43,9 @@ impl Day09 {
fn read_input(input: &mut Read) -> (usize, usize) {
let mut data = String::new();
input.read_to_string(&mut data).unwrap();
let mut parts = data.split(" ");
let mut parts = data.split(' ');
let elves = parts.next().unwrap().parse().unwrap();
let marbles = parts.skip(5).next().unwrap().parse().unwrap();
let marbles = parts.nth(5).unwrap().parse().unwrap();
(elves, marbles)
}

View File

@@ -86,6 +86,12 @@ impl Day11 {
}
}
impl Default for Day11 {
fn default() -> Self {
Self::new()
}
}
impl Solution for Day11 {
fn part1(&mut self, input: &mut Read) -> String {
let serial = read_single_input(input);

View File

@@ -20,7 +20,7 @@ fn char_bool(c: char) -> bool {
}
}
fn print_state(state: &State) -> String {
fn print_state(state: &[(i64, bool)]) -> String {
state.iter().map(|(_, x)| if *x { '#' } else { '.' }).collect()
}
@@ -60,7 +60,7 @@ impl Day12 {
state
}
fn simulate(&self, state: &State) -> State {
fn simulate(&self, state: &[(i64, bool)]) -> State {
let mut new_state = Vec::with_capacity(state.len() + 8);
let mut index = 0;
for &(idx, b) in state {
@@ -97,7 +97,7 @@ impl Day12 {
state
}
fn sum(&self, state: &State) -> i64 {
fn sum(&self, state: &[(i64, bool)]) -> i64 {
state.iter()
.filter(|&&(_, x)| x)
.map(|&(i, _)| i)
@@ -118,7 +118,7 @@ impl Solution for Day12 {
let mut state = self.read_input(input);
let mut seen = HashMap::new();
let mut time = 1i64;
const TARGET_TIME: i64 = 50000000000;
const TARGET_TIME: i64 = 50_000_000_000;
loop {
state = self.simulate(&state);

View File

@@ -25,7 +25,7 @@ impl Direction {
_ => panic!("Invalid direction {}", c),
}
}
pub fn clockwise(&self) -> Self {
pub fn clockwise(self) -> Self {
match self {
Direction::North => Direction::East,
Direction::East => Direction::South,
@@ -34,11 +34,11 @@ impl Direction {
}
}
pub fn counter_clockwise(&self) -> Self {
return self.clockwise().clockwise().clockwise();
pub fn counter_clockwise(self) -> Self {
self.clockwise().clockwise().clockwise()
}
pub fn run(&self, pos: Coordinate) -> Coordinate {
pub fn run(self, pos: Coordinate) -> Coordinate {
let (x, y) = pos;
match self {
Direction::North => (x, y - 1),

View File

@@ -46,7 +46,7 @@ fn skill_after(n: usize) -> u64 {
let mut skill = 0;
for d in state.skip(n).take(10) {
skill *= 10;
skill += d as u64;
skill += u64::from(d);
}
skill

View File

@@ -28,7 +28,7 @@ enum OpCode {
}
impl OpCode {
fn valid(&self, op: &[i32; 4], before: &[i32; 4], after: &[i32; 4]) -> bool {
fn valid(self, op: &[i32; 4], before: &[i32; 4], after: &[i32; 4]) -> bool {
let mut cpu: CPU = Default::default();
cpu.registers.copy_from_slice(before);
@@ -38,7 +38,7 @@ impl OpCode {
}
}
return false;
false
}
}
@@ -72,7 +72,7 @@ struct CPU {
}
impl CPU {
pub fn execute(&mut self, op: &OpCode, var: &[i32]) -> Result<i32, CPUErr> {
pub fn execute(&mut self, op: OpCode, var: &[i32]) -> Result<i32, CPUErr> {
use self::OpCode::*;
let res = match op {
ADDR => self.reg(var[0])? + self.reg(var[1])?,
@@ -121,7 +121,7 @@ impl Day16 {
fn read(&mut self, reader: &mut BufRead, target: &mut [i32; 4]) -> bool {
self.buf.clear();
if let Err(_) = reader.read_line(&mut self.buf) {
if reader.read_line(&mut self.buf).is_err() {
return false;
}
@@ -154,7 +154,7 @@ impl Day16 {
if mappings[op[0] as usize].is_empty() {
mappings[op[0] as usize].extend(OP_LIST.iter()
.filter(|x| x.valid(&op, &before, &after))
.map(|x| *x));
.cloned());
} else {
for option in OP_LIST.iter()
.filter(|x| !x.valid(&op, &before, &after)) {
@@ -197,6 +197,12 @@ impl Day16 {
}
}
impl Default for Day16 {
fn default() -> Self {
Self::new()
}
}
impl Solution for Day16 {
fn part1(&mut self, input: &mut Read) -> String {
let mut reader = BufReader::new(input);
@@ -235,7 +241,7 @@ impl Solution for Day16 {
let mut cpu: CPU = Default::default();
while self.read(&mut reader, &mut op) {
cpu.execute(&mapping[op[0] as usize], &op[1..4]).unwrap();
cpu.execute(mapping[op[0] as usize], &op[1..4]).unwrap();
}
format!("{}", cpu.registers[0])