diff --git a/Cargo.lock b/Cargo.lock index cb6c099..d40d4e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,18 +4,18 @@ version = 3 [[package]] name = "day1" -version = "0.1.0" +version = "2022.0.0" [[package]] name = "day10" -version = "0.1.0" +version = "2022.0.0" [[package]] name = "day11" -version = "0.1.0" +version = "2022.0.0" dependencies = [ "derive-getters", - "itertools 0.11.0", + "itertools", "log", "nom", ] @@ -24,7 +24,7 @@ dependencies = [ name = "day12" version = "0.1.0" dependencies = [ - "itertools 0.11.0", + "itertools", "log", ] @@ -32,43 +32,44 @@ dependencies = [ name = "day13" version = "2022.0.0" dependencies = [ + "log", "nom", ] [[package]] name = "day2" -version = "0.1.0" +version = "2022.0.0" [[package]] name = "day3" -version = "0.1.0" +version = "2022.0.0" [[package]] name = "day4" -version = "0.1.0" +version = "2022.0.0" [[package]] name = "day5" -version = "0.1.0" +version = "2022.0.0" [[package]] name = "day6" -version = "0.1.0" +version = "2022.0.0" [[package]] name = "day7" -version = "0.1.0" +version = "2022.0.0" [[package]] name = "day8" -version = "0.1.0" +version = "2022.0.0" dependencies = [ - "itertools 0.10.5", + "itertools", ] [[package]] name = "day9" -version = "0.1.0" +version = "2022.0.0" [[package]] name = "derive-getters" @@ -87,15 +88,6 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" -[[package]] -name = "itertools" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" -dependencies = [ - "either", -] - [[package]] name = "itertools" version = "0.11.0" diff --git a/Cargo.toml b/Cargo.toml index 5de6a79..7a860bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ members = [ "day12", "day13", ] +resolver = "2" [workspace.package] diff --git a/day1/Cargo.toml b/day1/Cargo.toml index a3c4e52..3033c17 100644 --- a/day1/Cargo.toml +++ b/day1/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "day1" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/day1/src/main.rs b/day1/src/main.rs index 4e9de6e..71b843a 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] + use std::fs::File; use std::io::{prelude::*, BufReader}; @@ -20,16 +22,13 @@ fn main() -> std::io::Result<()> { //order the elves since we don't care about position anymore elves.sort_by(|a, b| b.cmp(a)); - let max = *elves.get(0).expect("faliure"); + let max = *elves.first().expect("faliure"); let counts = elves.iter().take(3).sum::(); - //elves.sort(); - //let max = elves.len() - 1; //part 1 is get the max println!("Part 1: {max}"); //Part 2 is get the sum of the largest 3 - //let counts: u64 = elves[(max - 2)..].iter().sum(); println!("Part 2: {counts}"); Ok(()) diff --git a/day10/Cargo.toml b/day10/Cargo.toml index 40d2066..e659155 100644 --- a/day10/Cargo.toml +++ b/day10/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "day10" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/day10/src/main.rs b/day10/src/main.rs index e030e03..d141bc0 100644 --- a/day10/src/main.rs +++ b/day10/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] + use std::fs::File; use std::io::{prelude::*, BufReader}; use std::str; @@ -7,17 +9,17 @@ fn main() -> std::io::Result<()> { let file = File::open("input")?; let reader = BufReader::new(file); - let mut part1: Vec = Vec::new(); + let mut part1: Vec<_> = Vec::new(); let mut part2 = [[b'.'; 40]; 6]; let mut x_reg = 1; - let mut pc = 0; + let mut pc = 0_u32; reader.lines().for_each(|line| { let line = line.unwrap(); let op = match line.split(' ').collect::>()[..] { - ["addx", x] => Some(x.parse::().unwrap()), + ["addx", x] => Some(x.parse::().unwrap()), ["noop"] => None, - _ => panic!("invalid command: {}", line), + _ => panic!("invalid command: {line}"), }; let steps = if op.is_some() { 2 } else { 1 }; for i in 0..steps { @@ -38,7 +40,7 @@ fn main() -> std::io::Result<()> { } }); - println!("Part 1: {}", part1.iter().sum::()); + println!("Part 1: {}", part1.iter().sum::()); for row in part2 { println!("Part 2: {}", str::from_utf8(&row).unwrap()); } diff --git a/day11/Cargo.toml b/day11/Cargo.toml index 092bd7a..4a182aa 100644 --- a/day11/Cargo.toml +++ b/day11/Cargo.toml @@ -1,13 +1,15 @@ [package] name = "day11" -version = "0.1.0" -edition = "2021" description = "AOC 2022 Day 11" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -derive-getters = "0.3.0" -itertools = "0.11.0" -log = "0.4.20" -nom = "7.1.3" +derive-getters.workspace = true +itertools.workspace = true +log.workspace = true +nom.workspace = true diff --git a/day11/src/main.rs b/day11/src/main.rs index d189df9..74679de 100644 --- a/day11/src/main.rs +++ b/day11/src/main.rs @@ -1,6 +1,8 @@ #![warn(clippy::all, clippy::pedantic)] + use derive_getters::Getters; use itertools::Itertools; +use log::{debug, trace}; use nom::{ branch::alt, bytes::complete::tag, @@ -10,7 +12,6 @@ use nom::{ sequence::{delimited, preceded}, IResult, Parser, }; -use log::{debug, trace}; use std::{ collections::VecDeque, error, fmt::Display, fs::File, io::Read, num::ParseIntError, str::FromStr, @@ -150,8 +151,12 @@ impl Test { let (input, true_to) = match preceded( tag("If true: throw to monkey "), nom::character::complete::u64.map(|x| usize::try_from(x).unwrap()), - )(input){ - Err(e) => {println!("{e:?}"); Err(e)}, + )(input) + { + Err(e) => { + println!("{e:?}"); + Err(e) + } x => x, }?; trace!("parse test 4"); @@ -186,7 +191,7 @@ impl Ape { } pub fn inspect(&mut self, relief: bool) -> Option { - let relief: u64 = if relief {3} else {1}; + let relief: u64 = if relief { 3 } else { 1 }; let item = self.items.pop_front()?; self.inspected += 1; Some(self.operation.do_op(item) / relief) @@ -331,7 +336,7 @@ fn main() { //game 1 for _ in 0..20 { for i in 0..monkeys.len() { - while let Some( item) = monkeys[i].inspect(true){ + while let Some(item) = monkeys[i].inspect(true) { let throw_to = monkeys[i].throw(item); monkeys[throw_to].catch(item); } @@ -348,7 +353,10 @@ fn main() { .product::(); println!("Part 1: {val}"); - let magic = monkeys_game2.iter().map(|x| x.test().divisor()).product::(); + let magic = monkeys_game2 + .iter() + .map(|x| x.test().divisor()) + .product::(); for _ in 0..10_000 { for i in 0..monkeys_game2.len() { diff --git a/day2/Cargo.toml b/day2/Cargo.toml index 8aa34bb..b9c26aa 100644 --- a/day2/Cargo.toml +++ b/day2/Cargo.toml @@ -1,8 +1,9 @@ [package] name = "day2" -version = "0.1.0" -edition = "2021" - +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/day2/src/main.rs b/day2/src/main.rs index c70f211..8abbe8b 100644 --- a/day2/src/main.rs +++ b/day2/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] + use std::fs::File; use std::io::{prelude::*, BufReader}; @@ -61,7 +63,7 @@ impl std::str::FromStr for Game { let opponent: Choice = str_split[0].parse()?; // game1 //let you: Choice = str_split[1].parse()?; - let you = match str_split[1] { + let you = match *str_split.get(1).expect("msg") { "X" => opponent.beats(), "Y" => str_split[0].parse()?, "Z" => opponent.loses(), diff --git a/day3/Cargo.toml b/day3/Cargo.toml index 898e70d..0416447 100644 --- a/day3/Cargo.toml +++ b/day3/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "day3" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/day3/src/main.rs b/day3/src/main.rs index 6654da9..f297e34 100644 --- a/day3/src/main.rs +++ b/day3/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] + use std::fs::File; use std::io::{prelude::*, BufReader}; @@ -28,22 +30,21 @@ fn main() -> std::io::Result<()> { .lines() .fold(Vec::new(), |mut acc: Vec>, line| { if acc.is_empty() || acc.last().unwrap().len() == 3 { - acc.push(Vec::new()) + acc.push(Vec::new()); } acc.last_mut().unwrap().push(line.unwrap()); acc }) .iter() .map(|group| { - let (g1, g2, g3) = match group.as_slice() { - [g1, g2, g3] => (g1, g2, g3), - _ => panic!("not get here"), + let [g1, g2, g3] = group.as_slice() else { + panic!("not get here") }; match g1 .chars() .fold(Vec::new(), |mut combo: Vec, ch| { if g2.contains(ch) { - combo.push(ch) + combo.push(ch); } combo }) diff --git a/day4/Cargo.toml b/day4/Cargo.toml index 8842130..35259d2 100644 --- a/day4/Cargo.toml +++ b/day4/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "day4" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/day4/src/main.rs b/day4/src/main.rs index 71ad02f..d253324 100644 --- a/day4/src/main.rs +++ b/day4/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] + use std::fs::File; use std::io::{prelude::*, BufReader}; diff --git a/day5/Cargo.toml b/day5/Cargo.toml index f9b62bf..ddbf17c 100644 --- a/day5/Cargo.toml +++ b/day5/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "day5" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/day5/src/main.rs b/day5/src/main.rs index 7514f08..39abdda 100644 --- a/day5/src/main.rs +++ b/day5/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] + use std::fs::File; use std::io::{prelude::*, BufReader}; @@ -40,7 +42,7 @@ impl GameBoard { self.board .iter() .map(|x| x.last().unwrap().label.clone()) - .fold("".to_string(), |acc, x| acc + &x) + .fold(String::new(), |acc, x| acc + &x) } } impl From> for GameBoard { @@ -50,12 +52,12 @@ impl From> for GameBoard { // get labels let labels = label_vec .split_whitespace() - .map(|x| x.to_string()) + .map(std::string::ToString::to_string) .collect::>(); //TODO sscanf for crates board_vec.reverse(); let mut board = vec![Vec::new(); labels.len()]; - board_vec.iter().for_each(|line| { + for line in &board_vec { board.iter_mut().enumerate().for_each(|(i, col)| { let (begin, end) = (i * 4, std::cmp::min(i * 4 + 4, line.len())); let crate_str = line[begin..end] @@ -65,8 +67,8 @@ impl From> for GameBoard { if !crate_str.is_empty() { col.push(Crate { label: crate_str }); } - }) - }); + }); + } GameBoard { _labels: labels, board, @@ -94,10 +96,10 @@ fn main() -> std::io::Result<()> { acc }, ); - println!("{:?}", board_lines); + println!("{board_lines:?}"); let mut board = GameBoard::from(board_lines); - movement_lines.iter().for_each(|line| { + for line in &movement_lines { match line .split_whitespace() .filter_map(|x| x.parse::().ok()) @@ -112,7 +114,7 @@ fn main() -> std::io::Result<()> { .collect::>() ), }; - }); + } println!("{}", board.get_tops()); diff --git a/day6/Cargo.toml b/day6/Cargo.toml index 89d04ae..37796e7 100644 --- a/day6/Cargo.toml +++ b/day6/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "day6" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/day6/src/main.rs b/day6/src/main.rs index 32c7ef1..6bbffef 100644 --- a/day6/src/main.rs +++ b/day6/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] + use std::collections::HashMap; use std::fs::File; use std::io::{prelude::*, BufReader}; @@ -18,7 +20,7 @@ fn main() -> std::io::Result<()> { } holder.insert(c, true); } - println!("Part 1: line: {} marker at: {}", line_no, i); + println!("Part 1: line: {line_no} marker at: {i}"); break; } 'checker: for i in 14..line.len() { @@ -30,7 +32,7 @@ fn main() -> std::io::Result<()> { } holder.insert(c, true); } - println!("Part 2: line: {} marker at: {}", line_no, i); + println!("Part 2: line: {line_no} marker at: {i}"); break; } }); diff --git a/day7/Cargo.toml b/day7/Cargo.toml index b170ccb..80516c0 100644 --- a/day7/Cargo.toml +++ b/day7/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "day7" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/day7/src/main.rs b/day7/src/main.rs index 1718837..24e5e7c 100644 --- a/day7/src/main.rs +++ b/day7/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] + use std::cell::RefCell; use std::fs::File; use std::io::{prelude::*, BufReader}; @@ -15,7 +17,7 @@ impl Sizer for Vec { impl Sizer for Vec { fn size(&self) -> usize { - self.iter().map(|x| x.size()).sum() + self.iter().map(Sizer::size).sum() } } @@ -50,7 +52,7 @@ impl MyDir { .iter() .filter_map(|x| match x { FileSystemTypes::MyDir(y) => Some(y), - _ => None, + FileSystemTypes::MyFile(_) => None, }) .find(|x| *x.borrow().name == dir)? .clone(), @@ -63,7 +65,7 @@ impl MyDir { })); } //has to be a static method... - fn mkdir(self_: Rc>, name: impl Into) { + fn mkdir(self_: &Rc>, name: impl Into) { self_ .borrow_mut() .objects @@ -136,11 +138,11 @@ fn main() -> std::io::Result<()> { if ls_mode && line.as_bytes()[0] != b'$' { //do adding to cursor match line.split_whitespace().collect::>()[..] { - ["dir", name] => MyDir::mkdir(cursor.clone(), name), + ["dir", name] => MyDir::mkdir(&cursor.clone(), name), [size, name] => cursor .borrow_mut() .touch(name, size.parse::().unwrap()), - _ => panic!("oops {}", line), + _ => panic!("oops {line}"), }; // end the for_each return; @@ -155,13 +157,13 @@ fn main() -> std::io::Result<()> { ["$", "cd", "/"] => root.clone(), //set current directory back to root ["$", "cd", ".."] => cursor.borrow().move_up().unwrap(), ["$", "cd", dir] => cursor.borrow().move_down(dir).unwrap(), - _ => panic!("unknown command {}", line), + _ => panic!("unknown command {line}"), } }); let mut part1 = Vec::new(); let max = recurse_part1(&mut part1, &root.borrow()); let part1_ans: usize = part1.iter().filter(|x| **x <= 100_000).sum(); - println!("Part 1: {}", part1_ans); + println!("Part 1: {part1_ans}"); //part 2 let free_space = 70_000_000_usize - max; diff --git a/day8/Cargo.toml b/day8/Cargo.toml index eb57b15..34b1da9 100644 --- a/day8/Cargo.toml +++ b/day8/Cargo.toml @@ -1,9 +1,11 @@ [package] name = "day8" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -itertools = "0.10.5" +itertools.workspace = true diff --git a/day8/src/main.rs b/day8/src/main.rs index 24accc1..ff5a064 100644 --- a/day8/src/main.rs +++ b/day8/src/main.rs @@ -1,26 +1,32 @@ +#![warn(clippy::all, clippy::pedantic)] + use itertools::Itertools; use std::fs::File; use std::io::{prelude::*, BufReader}; -fn main() -> std::io::Result<()> { - //Read in file - let file = File::open("input")?; +fn get_board(file: &File) -> Vec> { let reader = BufReader::new(file); - let board = reader + reader .lines() .map(|line| { line.unwrap() .matches(char::is_numeric) - .map(|num| num.parse::().unwrap()) - .collect::>() + .map(|num| num.parse().unwrap()) + .collect() }) - .collect::>>(); + .collect() +} + +fn main() -> std::io::Result<()> { + //Read in file + let file = File::open("input")?; + + let board = get_board(&file); + let y_len = board.len(); - let x_len = board.iter().map(|x| x.len()).max().unwrap(); - if board.iter().any(|x| x.len() != x_len) { - panic!("board isn't square") - } + let x_len = board.iter().map(std::vec::Vec::len).max().unwrap(); + assert!(board.iter().any(|x| x.len() != x_len), "board isn't square"); let mut visible: Vec<(usize, usize, u8)> = Vec::new(); let mut max_in_row_from_left = vec![0_usize; y_len]; @@ -142,14 +148,14 @@ fn main() -> std::io::Result<()> { } } let part1 = visible.iter().unique().count(); - println!("Part 1: {}", part1); + println!("Part 1: {part1}"); let part2 = scores .iter() .map(|x| x.iter().max().unwrap()) .max() .unwrap(); - println!("Part 2: {}", part2); + println!("Part 2: {part2}"); Ok(()) } diff --git a/day9/Cargo.toml b/day9/Cargo.toml index b5e5273..5c8332a 100644 --- a/day9/Cargo.toml +++ b/day9/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "day9" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +authors.workspace = true +repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/day9/src/main.rs b/day9/src/main.rs index 2931476..d62f48c 100644 --- a/day9/src/main.rs +++ b/day9/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] + use std::collections::HashSet; use std::fs::File; use std::io::{prelude::*, BufReader}; @@ -7,7 +9,7 @@ fn main() -> std::io::Result<()> { let file = File::open("input")?; let reader = BufReader::new(file); - let mut snake = vec![(0_i32, 0_i32); 10]; + let mut snake = [(0_i32, 0_i32); 10]; let mut visited: Vec> = vec![HashSet::new(); 10]; //intialize the visited @@ -20,7 +22,7 @@ fn main() -> std::io::Result<()> { let line = line.unwrap(); let (direction, steps) = match line.split(' ').collect::>()[..] { [dir, step] => (dir, step.parse::().unwrap()), - _ => panic!("failed parseing line {}", line), + _ => panic!("failed parseing line {line}"), }; for _ in 0..steps { let (mut cur_head_x, mut cur_head_y) = snake[0]; @@ -29,7 +31,7 @@ fn main() -> std::io::Result<()> { "R" => cur_head_x += 1, "U" => cur_head_y += 1, "D" => cur_head_y -= 1, - x => panic!("invalid movement {}", x), + x => panic!("invalid movement {x}"), }; let new_head_pos = (cur_head_x, cur_head_y); snake[0] = new_head_pos; @@ -60,8 +62,8 @@ fn main() -> std::io::Result<()> { } }); let part1 = visited[1].len(); - println!("Part 1: {}", part1); + println!("Part 1: {part1}"); let part2 = visited[9].len(); - println!("Part 2: {}", part2); + println!("Part 2: {part2}"); Ok(()) }