cargo clippy fixes
This commit is contained in:
parent
818271e4ad
commit
3a72256177
17 changed files with 55 additions and 64 deletions
12
src/day01.rs
12
src/day01.rs
|
@ -10,9 +10,9 @@ pub fn input_generator(input: &str) -> Vec<usize> {
|
|||
}
|
||||
|
||||
#[aoc(day1, part1)]
|
||||
pub fn solve_part1(input: &Vec<usize>) -> usize {
|
||||
let mut input = input.clone();
|
||||
input.sort();
|
||||
pub fn solve_part1(input: &[usize]) -> usize {
|
||||
let mut input = input.to_vec();
|
||||
input.sort_unstable();
|
||||
for (i1, v1) in input.iter().enumerate() {
|
||||
if v1 > &2020 {
|
||||
break;
|
||||
|
@ -27,9 +27,9 @@ pub fn solve_part1(input: &Vec<usize>) -> usize {
|
|||
}
|
||||
|
||||
#[aoc(day1, part2)]
|
||||
pub fn solve_part2(input: &Vec<usize>) -> usize {
|
||||
let mut input = input.clone();
|
||||
input.sort();
|
||||
pub fn solve_part2(input: &[usize]) -> usize {
|
||||
let mut input = input.to_vec();
|
||||
input.sort_unstable();
|
||||
for (i1, v1) in input.iter().enumerate() {
|
||||
if v1 > &2020 {
|
||||
break;
|
||||
|
|
|
@ -53,11 +53,11 @@ pub fn input_generator(input: &str) -> Vec<Rule> {
|
|||
}
|
||||
|
||||
#[aoc(day2, part1)]
|
||||
pub fn solve_part1(input: &Vec<Rule>) -> usize {
|
||||
pub fn solve_part1(input: &[Rule]) -> usize {
|
||||
return input.iter().filter(|v| v.is_valid_part1()).count();
|
||||
}
|
||||
|
||||
#[aoc(day2, part2)]
|
||||
pub fn solve_part2(input: &Vec<Rule>) -> usize {
|
||||
pub fn solve_part2(input: &[Rule]) -> usize {
|
||||
return input.iter().filter(|v| v.is_valid_part2()).count();
|
||||
}
|
||||
|
|
20
src/day04.rs
20
src/day04.rs
|
@ -42,11 +42,7 @@ impl Creds {
|
|||
return false;
|
||||
}
|
||||
if let Ok(val) = val.parse::<i64>() {
|
||||
if val >= min && val <= max {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return val >= min && val <= max;
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
@ -61,10 +57,10 @@ impl Creds {
|
|||
let num: Result<usize, _> = num.parse();
|
||||
if let Ok(num) = num {
|
||||
if unit == "cm" {
|
||||
return num >= 150 && num <= 193;
|
||||
return (150..=193).contains(&num);
|
||||
};
|
||||
if unit == "in" {
|
||||
return num >= 59 && num <= 76;
|
||||
return (59..=76).contains(&num);
|
||||
};
|
||||
}
|
||||
return false;
|
||||
|
@ -72,7 +68,7 @@ impl Creds {
|
|||
|
||||
fn validate_ecl(val: &str) -> bool {
|
||||
let choices: Vec<&str> = vec!["amb", "blu", "brn", "gry", "grn", "hzl", "oth"];
|
||||
return choices.iter().find(|&&v| v == val).is_some();
|
||||
return choices.iter().any(|&v| v == val);
|
||||
}
|
||||
|
||||
fn validate_hcl(val: &str) -> bool {
|
||||
|
@ -121,10 +117,10 @@ pub fn input_generator(input: &str) -> Vec<Creds> {
|
|||
let mut ret = vec![];
|
||||
for creds in input.split("\n\n") {
|
||||
let creds = creds.replace("\n", " ");
|
||||
let creds = creds.split(" ");
|
||||
let creds = creds.split(' ');
|
||||
let mut cred_data = Creds::default();
|
||||
for cred in creds {
|
||||
let cred: Vec<&str> = cred.split(":").collect();
|
||||
let cred: Vec<&str> = cred.split(':').collect();
|
||||
let (key, value) = (cred[0], cred[1]);
|
||||
match key {
|
||||
"byr" => {
|
||||
|
@ -162,11 +158,11 @@ pub fn input_generator(input: &str) -> Vec<Creds> {
|
|||
}
|
||||
|
||||
#[aoc(day4, part1)]
|
||||
pub fn solve_part1(input: &Vec<Creds>) -> usize {
|
||||
pub fn solve_part1(input: &[Creds]) -> usize {
|
||||
return input.iter().filter(|v| v.is_valid_part1()).count();
|
||||
}
|
||||
|
||||
#[aoc(day4, part2)]
|
||||
pub fn solve_part2(input: &Vec<Creds>) -> usize {
|
||||
pub fn solve_part2(input: &[Creds]) -> usize {
|
||||
return input.iter().filter(|v| v.is_valid_part2()).count();
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ pub fn input_generator(input: &str) -> Vec<Vec<Direction>> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
pub fn seat_to_pos(seat: &Vec<Direction>) -> (usize, usize) {
|
||||
pub fn seat_to_pos(seat: &[Direction]) -> (usize, usize) {
|
||||
let mut col_range: (usize, usize) = (0, 8);
|
||||
let mut row_range: (usize, usize) = (0, 128);
|
||||
for inst in seat {
|
||||
|
@ -57,7 +57,7 @@ pub fn seat_to_pos(seat: &Vec<Direction>) -> (usize, usize) {
|
|||
}
|
||||
|
||||
#[aoc(day5, part1)]
|
||||
pub fn solve_part1(input: &Vec<Vec<Direction>>) -> usize {
|
||||
pub fn solve_part1(input: &[Vec<Direction>]) -> usize {
|
||||
let mut max_seat_id: usize = 0;
|
||||
for seat in input {
|
||||
let pos = seat_to_pos(seat);
|
||||
|
@ -68,7 +68,7 @@ pub fn solve_part1(input: &Vec<Vec<Direction>>) -> usize {
|
|||
}
|
||||
|
||||
#[aoc(day5, part2)]
|
||||
pub fn solve_part2(input: &Vec<Vec<Direction>>) -> usize {
|
||||
pub fn solve_part2(input: &[Vec<Direction>]) -> usize {
|
||||
let mut seats = [[false; 8]; 128];
|
||||
for seat in input {
|
||||
let pos = seat_to_pos(seat);
|
||||
|
|
|
@ -6,7 +6,7 @@ pub fn input_generator(input: &str) -> Vec<Vec<HashSet<char>>> {
|
|||
.split("\n\n")
|
||||
.map(|block| {
|
||||
block
|
||||
.split("\n")
|
||||
.split('\n')
|
||||
.map(|line| line.chars().collect())
|
||||
.collect()
|
||||
})
|
||||
|
@ -14,7 +14,7 @@ pub fn input_generator(input: &str) -> Vec<Vec<HashSet<char>>> {
|
|||
}
|
||||
|
||||
#[aoc(day6, part1)]
|
||||
fn solve_part1(data: &Vec<Vec<HashSet<char>>>) -> usize {
|
||||
fn solve_part1(data: &[Vec<HashSet<char>>]) -> usize {
|
||||
let mut total = 0;
|
||||
for group in data {
|
||||
let mut answers: HashSet<char> = HashSet::new();
|
||||
|
@ -27,7 +27,7 @@ fn solve_part1(data: &Vec<Vec<HashSet<char>>>) -> usize {
|
|||
}
|
||||
|
||||
#[aoc(day6, part2)]
|
||||
fn solve_part2(data: &Vec<Vec<HashSet<char>>>) -> usize {
|
||||
fn solve_part2(data: &[Vec<HashSet<char>>]) -> usize {
|
||||
let mut total = 0;
|
||||
for group in data {
|
||||
let mut answers: HashSet<char> = HashSet::new();
|
||||
|
@ -35,7 +35,7 @@ fn solve_part2(data: &Vec<Vec<HashSet<char>>>) -> usize {
|
|||
answers.extend(person);
|
||||
}
|
||||
for person in group {
|
||||
answers = answers.intersection(&person).map(|v| *v).collect();
|
||||
answers = answers.intersection(&person).copied().collect();
|
||||
}
|
||||
total += answers.len();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Data {
|
||||
fwd: HashMap<String, Vec<(usize, String)>>,
|
||||
rev: HashMap<String, Vec<(usize, String)>>,
|
||||
|
@ -8,10 +9,7 @@ pub struct Data {
|
|||
|
||||
impl Data {
|
||||
pub fn new() -> Data {
|
||||
Data {
|
||||
fwd: HashMap::new(),
|
||||
rev: HashMap::new(),
|
||||
}
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -49,10 +49,10 @@ impl From<&str> for Inst {
|
|||
}
|
||||
|
||||
impl CPU {
|
||||
fn new(pgm: &Vec<Inst>) -> Self {
|
||||
fn new(pgm: &[Inst]) -> Self {
|
||||
Self {
|
||||
ip: 0,
|
||||
program: pgm.clone(),
|
||||
program: pgm.to_vec(),
|
||||
acc: 0,
|
||||
}
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ pub fn input_generator(input: &str) -> Vec<Inst> {
|
|||
|
||||
#[aoc(day8, part1)]
|
||||
#[inline(always)]
|
||||
pub fn solve_part1(input: &Vec<Inst>) -> usize {
|
||||
pub fn solve_part1(input: &[Inst]) -> usize {
|
||||
let mut seen = HashSet::new();
|
||||
let mut cpu = CPU::new(input);
|
||||
loop {
|
||||
|
@ -103,7 +103,7 @@ pub fn solve_part1(input: &Vec<Inst>) -> usize {
|
|||
|
||||
#[aoc(day8, part2)]
|
||||
#[inline(always)]
|
||||
pub fn solve_part2(input: &Vec<Inst>) -> usize {
|
||||
pub fn solve_part2(input: &[Inst]) -> usize {
|
||||
for (idx, inst) in input.iter().enumerate() {
|
||||
match inst {
|
||||
Inst::Nop { .. } | Inst::Jmp { .. } => {
|
||||
|
|
|
@ -12,8 +12,8 @@ pub fn input_generator(input: &str) -> Vec<usize> {
|
|||
|
||||
#[aoc(day9, part1)]
|
||||
#[inline(always)]
|
||||
pub fn solve_part1(input: &Vec<usize>) -> usize {
|
||||
for (n, win) in input.as_slice().windows(25).enumerate() {
|
||||
pub fn solve_part1(input: &[usize]) -> usize {
|
||||
for (n, win) in input.windows(25).enumerate() {
|
||||
let n = n + 25;
|
||||
if n >= input.len() {
|
||||
continue;
|
||||
|
@ -39,7 +39,7 @@ pub fn solve_part1(input: &Vec<usize>) -> usize {
|
|||
|
||||
#[aoc(day9, part2)]
|
||||
#[inline(always)]
|
||||
pub fn solve_part2_fast(input: &Vec<usize>) -> usize {
|
||||
pub fn solve_part2_fast(input: &[usize]) -> usize {
|
||||
let target_num = solve_part1(input);
|
||||
for i in 2..=input.len() {
|
||||
if let Some(w) = input.windows(i).find(|w| target_num == w.iter().sum()) {
|
||||
|
|
|
@ -45,7 +45,7 @@ impl Data {
|
|||
let mut nodes = self.0.clone();
|
||||
nodes.push(0);
|
||||
nodes.push(nodes.iter().max().unwrap() + 3);
|
||||
nodes.sort();
|
||||
nodes.sort_unstable();
|
||||
let goal = *nodes.last().unwrap();
|
||||
let mut map: HashMap<u16, HashSet<u16>> = HashMap::new();
|
||||
for n in &nodes {
|
||||
|
|
|
@ -137,7 +137,7 @@ pub fn input_generator(input: &str) -> Vec<Command> {
|
|||
}
|
||||
|
||||
#[aoc(day12, part1)]
|
||||
pub fn solve_part1(input: &Vec<Command>) -> usize {
|
||||
pub fn solve_part1(input: &[Command]) -> usize {
|
||||
let mut ship = Ship::new();
|
||||
for cmd in input {
|
||||
ship.cmd_p1(cmd);
|
||||
|
@ -146,7 +146,7 @@ pub fn solve_part1(input: &Vec<Command>) -> usize {
|
|||
}
|
||||
|
||||
#[aoc(day12, part2)]
|
||||
pub fn solve_part2(input: &Vec<Command>) -> usize {
|
||||
pub fn solve_part2(input: &[Command]) -> usize {
|
||||
let mut ship = Ship::new();
|
||||
for cmd in input {
|
||||
ship.cmd_p2(cmd);
|
||||
|
|
|
@ -52,10 +52,10 @@ pub fn solve_part2(input: &Data) -> i64 {
|
|||
.map(|(a, b)| (a as i64, *b as i64))
|
||||
.collect::<Vec<_>>();
|
||||
let mut t = 0;
|
||||
let target = b.iter().map(|(_, b)| b).fold(1, |a, b| a * b);
|
||||
let target: i64 = b.iter().map(|(_, b)| b).product();
|
||||
loop {
|
||||
let mut cnt = 0;
|
||||
let dt = b
|
||||
let dt: i64= b
|
||||
.iter()
|
||||
.filter_map(|(i, n)| {
|
||||
if (t + i) % n == 0 {
|
||||
|
@ -65,7 +65,7 @@ pub fn solve_part2(input: &Data) -> i64 {
|
|||
None
|
||||
}
|
||||
})
|
||||
.fold(1, |a, b| a * b);
|
||||
.product();
|
||||
if dt == target {
|
||||
return t % dt;
|
||||
}
|
||||
|
|
12
src/day14.rs
12
src/day14.rs
|
@ -27,8 +27,8 @@ impl Command {
|
|||
panic!("Can't apply non-mask command");
|
||||
}
|
||||
|
||||
fn apply_mask_v2_inner(&self, addr: &mut u64, s: &Vec<char>, ret: &mut Vec<u64>) {
|
||||
let mut s_c = s.clone();
|
||||
fn apply_mask_v2_inner(&self, addr: &mut u64, s: &[char], ret: &mut Vec<u64>) {
|
||||
let mut s_c = s.to_vec();
|
||||
for (idx, c) in s.iter().enumerate() {
|
||||
match *c {
|
||||
'0' => (),
|
||||
|
@ -55,7 +55,7 @@ impl Command {
|
|||
raw.reverse();
|
||||
let mut ret = vec![];
|
||||
self.apply_mask_v2_inner(&mut addr, &raw, &mut ret);
|
||||
ret.sort();
|
||||
ret.sort_unstable();
|
||||
ret.dedup();
|
||||
return ret;
|
||||
};
|
||||
|
@ -73,7 +73,7 @@ pub fn input_generator(input: &str) -> Data {
|
|||
let mut mask_and = 0;
|
||||
let mut mask_or = 0;
|
||||
let mut char_buf = vec![];
|
||||
for (_, c) in line.split("=").nth(1).unwrap().trim().chars().enumerate() {
|
||||
for (_, c) in line.split('=').nth(1).unwrap().trim().chars().enumerate() {
|
||||
char_buf.push(c);
|
||||
mask_and <<= 1;
|
||||
mask_or <<= 1;
|
||||
|
@ -126,7 +126,7 @@ pub fn input_generator(input: &str) -> Data {
|
|||
}
|
||||
|
||||
#[aoc(day14, part1)]
|
||||
pub fn solve_part1(input: &Data) -> u64 {
|
||||
pub fn solve_part1(input: &[Command]) -> u64 {
|
||||
let mut mem = HashMap::new();
|
||||
let mut mask = Command::Mask {
|
||||
and: std::u64::MAX,
|
||||
|
@ -147,7 +147,7 @@ pub fn solve_part1(input: &Data) -> u64 {
|
|||
}
|
||||
|
||||
#[aoc(day14, part2)]
|
||||
pub fn solve_part2(input: &Data) -> u64 {
|
||||
pub fn solve_part2(input: &[Command]) -> u64 {
|
||||
let mut mem = HashMap::new();
|
||||
let mut mask = Command::Mask {
|
||||
and: std::u64::MAX,
|
||||
|
|
|
@ -36,7 +36,7 @@ impl Seen {
|
|||
#[aoc_generator(day15)]
|
||||
pub fn input_generator(input: &str) -> Data {
|
||||
input
|
||||
.split(",")
|
||||
.split(',')
|
||||
.map(|num| num.parse())
|
||||
.collect::<Result<Vec<usize>, _>>()
|
||||
.unwrap()
|
||||
|
@ -56,11 +56,11 @@ fn get_number(start: &[usize], num_turns: usize) -> usize {
|
|||
}
|
||||
|
||||
#[aoc(day15, part1)]
|
||||
pub fn solve_part1(input: &Data) -> usize {
|
||||
pub fn solve_part1(input: &[usize]) -> usize {
|
||||
return get_number(input, 2020);
|
||||
}
|
||||
|
||||
#[aoc(day15, part2)]
|
||||
pub fn solve_part2(input: &Data) -> usize {
|
||||
pub fn solve_part2(input: &[usize]) -> usize {
|
||||
return get_number(input, 30_000_000);
|
||||
}
|
||||
|
|
10
src/day16.rs
10
src/day16.rs
|
@ -13,7 +13,7 @@ type Data = Input;
|
|||
pub fn input_generator(input: &str) -> Data {
|
||||
let l = input.lines();
|
||||
let ranges: HashMap<String, Vec<(usize, usize)>> = l
|
||||
.take_while(|l| l.trim().len() != 0)
|
||||
.take_while(|l| !l.trim().is_empty())
|
||||
.map(|s| {
|
||||
let parts: Vec<_> = s.to_owned().split(": ").map(|v| v.to_owned()).collect();
|
||||
let name = parts.get(0).unwrap().trim().to_owned();
|
||||
|
@ -25,7 +25,7 @@ pub fn input_generator(input: &str) -> Data {
|
|||
.map(|l| {
|
||||
let r: Vec<usize> = l
|
||||
.to_owned()
|
||||
.split("-")
|
||||
.split('-')
|
||||
.map(|v| v.to_owned().parse().unwrap())
|
||||
.collect();
|
||||
(r[0], r[1])
|
||||
|
@ -38,12 +38,12 @@ pub fn input_generator(input: &str) -> Data {
|
|||
let my_ticket = l
|
||||
.next()
|
||||
.unwrap()
|
||||
.split(",")
|
||||
.split(',')
|
||||
.map(|v| v.parse().unwrap())
|
||||
.collect();
|
||||
let other_tickets = l
|
||||
.skip(2)
|
||||
.map(|nums| nums.split(",").map(|v| v.parse().unwrap()).collect())
|
||||
.map(|nums| nums.split(',').map(|v| v.parse().unwrap()).collect())
|
||||
.collect();
|
||||
Data {
|
||||
ranges,
|
||||
|
@ -52,7 +52,7 @@ pub fn input_generator(input: &str) -> Data {
|
|||
}
|
||||
}
|
||||
|
||||
fn in_ranges(num: usize, ranges: &Vec<(usize, usize)>) -> bool {
|
||||
fn in_ranges(num: usize, ranges: &[(usize, usize)]) -> bool {
|
||||
let mut in_range = false;
|
||||
for (min, max) in ranges {
|
||||
in_range |= (num >= *min) && (num <= *max);
|
||||
|
|
|
@ -37,9 +37,7 @@ impl Data {
|
|||
}
|
||||
let nb = self.count_neighbors(nx, ny, nz);
|
||||
let is_active = self.active.contains(&(nx, ny, nz));
|
||||
if is_active && (nb == 2 || nb == 3) {
|
||||
next_active.insert((nx, ny, nz));
|
||||
} else if !is_active && nb == 3 {
|
||||
if nb == 3 || is_active && nb == 2 {
|
||||
next_active.insert((nx, ny, nz));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,9 +40,7 @@ impl Data {
|
|||
}
|
||||
let nb = self.count_neighbors(nx, ny, nz, nw);
|
||||
let is_active = self.active.contains(&(nx, ny, nz, nw));
|
||||
if is_active && (nb == 2 || nb == 3) {
|
||||
next_active.insert((nx, ny, nz, nw));
|
||||
} else if !is_active && nb == 3 {
|
||||
if nb == 3 || is_active && nb == 2 {
|
||||
next_active.insert((nx, ny, nz, nw));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#![allow(clippy::clippy::needless_return)]
|
||||
use aoc_runner_derive::aoc_lib;
|
||||
pub mod day01;
|
||||
pub mod day02;
|
||||
|
|
Loading…
Reference in a new issue