1
0
mirror of https://gitlab.com/MisterBiggs/aoc_2015-rust.git synced 2025-06-16 06:56:42 +00:00

fixed clippy recommentations

This commit is contained in:
Anson 2022-11-05 22:46:38 -06:00
parent 78c999f90b
commit 2e7336b959
8 changed files with 46 additions and 62 deletions

2
Cargo.lock generated
View File

@ -3,7 +3,7 @@
version = 3
[[package]]
name = "AoC2015"
name = "aoc_2015"
version = "0.1.0"
dependencies = [
"itertools",

View File

@ -1,5 +1,5 @@
[package]
name = "AoC2015"
name = "aoc_2015"
version = "0.1.0"
edition = "2021"

View File

@ -2,15 +2,13 @@ use std::fs;
pub fn run() {
println!("Day 1:");
let input = fs::read_to_string("./inputs/day1.txt")
.expect("Could not read file")
.to_string();
let input = fs::read_to_string("./inputs/day1.txt").expect("Could not read file");
println!("\tPart 1: {}", part1(&input));
println!("\tPart 2: {}", part2(&input));
}
fn part1(directions: &String) -> isize {
fn part1(directions: &str) -> isize {
directions
.chars()
.map(|c| match c {
@ -20,7 +18,7 @@ fn part1(directions: &String) -> isize {
})
.sum::<isize>()
}
fn part2(directions: &String) -> usize {
fn part2(directions: &str) -> usize {
let mut floor: isize = 0;
for (num, dir) in directions.chars().enumerate() {
@ -35,7 +33,7 @@ fn part2(directions: &String) -> usize {
}
}
return directions.len() + 1;
directions.len() + 1
}
#[cfg(test)]

View File

@ -24,7 +24,7 @@ fn parse(input: String) -> Vec<Vec<usize>> {
fn part1(dimensions: &Vec<Vec<usize>>) -> usize {
dimensions
.into_iter()
.iter()
.map(|x| {
let (l, w, h) = (x[0], x[1], x[2]);
let slack = { cmp::min(l * w, cmp::min(w * h, l * h)) };
@ -35,7 +35,7 @@ fn part1(dimensions: &Vec<Vec<usize>>) -> usize {
fn part2(dimensions: &Vec<Vec<usize>>) -> usize {
dimensions
.into_iter()
.iter()
.map(|x| {
let (l, w, h) = (x[0], x[1], x[2]);
let volume = l * w * h;

View File

@ -31,36 +31,36 @@ pub fn run() {
println!("\tPart 2: {}", part2(&input));
}
fn part1(directions: &String) -> usize {
fn part1(directions: &str) -> usize {
let mut location = Point::new(0, 0);
let mut prev_locs = HashSet::new();
prev_locs.insert(location.clone());
prev_locs.insert(location);
for dir in directions.chars() {
location.next_point(dir);
prev_locs.insert(location.clone());
prev_locs.insert(location);
}
return prev_locs.len();
prev_locs.len()
}
fn part2(directions: &String) -> usize {
fn part2(directions: &str) -> usize {
let mut santa = Point::new(0, 0);
let mut robo = Point::new(0, 0);
let mut prev_locs = HashSet::new();
prev_locs.insert(santa.clone());
prev_locs.insert(santa);
for (i, dir) in directions.chars().enumerate() {
if i % 2 == 0 {
santa.next_point(dir);
prev_locs.insert(santa.clone());
prev_locs.insert(santa);
} else {
robo.next_point(dir);
prev_locs.insert(robo.clone());
prev_locs.insert(robo);
}
}
return prev_locs.len();
prev_locs.len()
}
#[cfg(test)]
@ -69,14 +69,14 @@ mod tests {
#[test]
fn test_part_1() {
assert_eq!(part1(&">".to_string()), 2);
assert_eq!(part1(&"^>v<".to_string()), 4);
assert_eq!(part1(&"^v^v^v^v^v".to_string()), 2);
assert_eq!(part1(">"), 2);
assert_eq!(part1("^>v<"), 4);
assert_eq!(part1("^v^v^v^v^v"), 2);
}
#[test]
fn test_part_2() {
assert_eq!(part2(&">>".to_string()), 2);
assert_eq!(part2(&"^>v<".to_string()), 3);
assert_eq!(part2(&"^v^v^v^v^v".to_string()), 11);
assert_eq!(part2(">>"), 2);
assert_eq!(part2("^>v<"), 3);
assert_eq!(part2("^v^v^v^v^v"), 11);
}
}

View File

@ -1,8 +1,6 @@
use md5;
pub fn run() {
println!("Day 4: [PRECALCULATED]");
let input = "yzbqklnj".to_string();
// let input = "yzbqklnj".to_string();
println!("\tPart 1: 282749");
println!("\tPart 2: 9962624");
@ -10,7 +8,8 @@ pub fn run() {
// println!("\tPart 2: {}", part1(&input, 6));
}
fn part1(input: &String, zeros: usize) -> usize {
#[allow(dead_code)]
fn part1(input: &str, zeros: usize) -> usize {
let mut key: usize = 0;
let max_iter = 1e9 as usize;
@ -35,7 +34,7 @@ mod tests {
#[test]
fn test_part_1() {
assert_eq!(part1(&"abcdef".to_string(), 5), 609043);
assert_eq!(part1(&"pqrstuv".to_string(), 5), 1048970);
assert_eq!(part1("abcdef", 5), 609043);
assert_eq!(part1("pqrstuv", 5), 1048970);
}
}

View File

@ -4,16 +4,14 @@ use itertools::Itertools;
pub fn run() {
println!("Day 5:");
let input = fs::read_to_string("./inputs/day5.txt")
.expect("Could not read file")
.to_string();
let input = fs::read_to_string("./inputs/day5.txt").expect("Could not read file");
// dbg!(input.)
println!("\tPart 1: {}", part1(&input));
println!("\tPart 2: {}", part2(&input));
}
fn part1(input: &String) -> usize {
fn part1(input: &str) -> usize {
let mut nice = 0;
for txt in input.split_whitespace() {
@ -53,10 +51,10 @@ fn part1(input: &String) -> usize {
nice += 1;
}
return nice;
nice
}
fn part2(input: &String) -> usize {
fn part2(input: &str) -> usize {
let mut nice = 0;
for txt in input.split_whitespace() {
@ -67,12 +65,10 @@ fn part2(input: &String) -> usize {
.enumerate()
.tuple_combinations()
{
if l.1 == r.1 {
if l.0.abs_diff(r.0) > 1 {
if l.1 == r.1 && l.0.abs_diff(r.0) > 1 {
double = true;
}
}
}
if !double {
continue;
}
@ -88,7 +84,7 @@ fn part2(input: &String) -> usize {
}
}
return nice;
nice
}
#[cfg(test)]
@ -97,12 +93,9 @@ mod tests {
#[test]
fn test_1_examples() {
assert_eq!(part1(&"ugknbfddgicrmopn".to_string()), 1);
assert_eq!(part1("ugknbfddgicrmopn"), 1);
assert_eq!(
part1(
&"ugknbfddgicrmopn\naaa\njchzalrnumimnmhp\nhaegwjzuvuyypxyu\ndvszwmarrgswjxmb"
.to_string()
),
part1("ugknbfddgicrmopn\naaa\njchzalrnumimnmhp\nhaegwjzuvuyypxyu\ndvszwmarrgswjxmb"),
2
);
}
@ -110,34 +103,26 @@ mod tests {
#[test]
fn test_1_input() {
assert_eq!(
part1(
&fs::read_to_string("./inputs/day5.txt")
.expect("Could not read file")
.to_string()
),
part1(&fs::read_to_string("./inputs/day5.txt").expect("Could not read file")),
238
);
}
#[test]
fn test_2_examples() {
assert_eq!(part2(&"qjhvhtzxzqqjkmpb".to_string()), 1);
assert_eq!(part2(&"xxyxx".to_string()), 1);
assert_eq!(part2(&"aaa".to_string()), 0);
assert_eq!(part2(&"aaaaa".to_string()), 1);
assert_eq!(part2("qjhvhtzxzqqjkmpb"), 1);
assert_eq!(part2("xxyxx"), 1);
assert_eq!(part2("aaa"), 0);
assert_eq!(part2("aaaaa"), 1);
assert_eq!(
part2(&"qjhvhtzxzqqjkmpb\nxxyxx\nuurcxstgmygtbstg\nieodomkazucvgmuy".to_string()),
part2("qjhvhtzxzqqjkmpb\nxxyxx\nuurcxstgmygtbstg\nieodomkazucvgmuy"),
2
);
}
#[test]
fn test_2_input() {
assert_eq!(
part2(
&fs::read_to_string("./inputs/day5.txt")
.expect("Could not read file")
.to_string()
),
part2(&fs::read_to_string("./inputs/day5.txt").expect("Could not read file")),
69
);
}

View File

@ -3,6 +3,7 @@ mod day2;
mod day3;
mod day4;
mod day5;
// mod day6;
fn main() {
println!("Running Advent of Code 2021");
@ -11,4 +12,5 @@ fn main() {
day3::run();
day4::run();
day5::run();
// day6::run();
}