From 49009dfada76dca299fb0e392a948f1344de2ba9 Mon Sep 17 00:00:00 2001 From: Dylan Thies Date: Fri, 29 Dec 2023 08:57:56 -0500 Subject: [PATCH] rustfmt 2023 --- 2023/day-1/src/part1.rs | 5 +++- 2023/day-1/src/part2.rs | 11 ++++++-- 2023/day-15/src/part1.rs | 7 +----- 2023/day-17/src/part1.rs | 5 +--- 2023/day-17/src/part2.rs | 43 +++++++++++++++++++++----------- 2023/day-23/src/part1.rs | 54 +++++++++++++++++++++++++++------------- 2023/day-23/src/part2.rs | 54 +++++++++++++++++++++++++++------------- 2023/day-4/src/part1.rs | 1 - 2023/day-7/src/part2.rs | 30 +++++++++++----------- 2023/day-9/src/part1.rs | 1 - 2023/day-9/src/part2.rs | 1 - 11 files changed, 133 insertions(+), 79 deletions(-) diff --git a/2023/day-1/src/part1.rs b/2023/day-1/src/part1.rs index 59e06eb..5b109ad 100644 --- a/2023/day-1/src/part1.rs +++ b/2023/day-1/src/part1.rs @@ -23,7 +23,10 @@ pub fn part1(input: &str) -> nom::IResult<&str, String> { "", values .iter() - .map(|v| v.first().expect("always at least one number") * 10 + v.last().expect("always atleast one number")) + .map(|v| { + v.first().expect("always at least one number") * 10 + + v.last().expect("always atleast one number") + }) .sum::() .to_string(), )) diff --git a/2023/day-1/src/part2.rs b/2023/day-1/src/part2.rs index 11991ac..bb2bccf 100644 --- a/2023/day-1/src/part2.rs +++ b/2023/day-1/src/part2.rs @@ -12,7 +12,10 @@ pub fn part2(input: &str) -> String { println!("{values:?}"); values .iter() - .map(|v| v.first().expect("There is always at least one number") * 10 + v.last().expect("there is always at least one number")) + .map(|v| { + v.first().expect("There is always at least one number") * 10 + + v.last().expect("there is always at least one number") + }) .sum::() .to_string() } @@ -42,7 +45,11 @@ fn parse_line(line: &str) -> Vec { } else if reduced_line.starts_with("zero") { Some(0) } else { - reduced_line.chars().next().expect("there is alwayss a character").to_digit(10) + reduced_line + .chars() + .next() + .expect("there is alwayss a character") + .to_digit(10) }; result diff --git a/2023/day-15/src/part1.rs b/2023/day-15/src/part1.rs index ec6ee6a..ae8b8d8 100644 --- a/2023/day-15/src/part1.rs +++ b/2023/day-15/src/part1.rs @@ -11,12 +11,7 @@ pub fn part1(input: &str) -> String { input .lines() - .map(|x| { - x.split(',') - .map(unhash) - .sum::() - .to_string() - }) + .map(|x| x.split(',').map(unhash).sum::().to_string()) .next() .unwrap() } diff --git a/2023/day-17/src/part1.rs b/2023/day-17/src/part1.rs index 15d2835..b1ba1e1 100644 --- a/2023/day-17/src/part1.rs +++ b/2023/day-17/src/part1.rs @@ -1,8 +1,6 @@ #![warn(clippy::all, clippy::pedantic)] -use std::{ - collections::{HashMap, VecDeque}, -}; +use std::collections::{HashMap, VecDeque}; use glam::IVec2; use pathfinding::prelude::dijkstra; @@ -106,4 +104,3 @@ mod test { assert_eq!(result, "102".to_string()); } } - diff --git a/2023/day-17/src/part2.rs b/2023/day-17/src/part2.rs index e593b33..c9093ae 100644 --- a/2023/day-17/src/part2.rs +++ b/2023/day-17/src/part2.rs @@ -1,8 +1,6 @@ #![warn(clippy::all, clippy::pedantic)] -use std::{ - collections::{HashMap, VecDeque}, -}; +use std::collections::{HashMap, VecDeque}; use glam::IVec2; use pathfinding::prelude::dijkstra; @@ -46,7 +44,7 @@ pub fn part2(input: &str) -> String { //let b = (next_lasts[3] - next_lasts[2]).signum(); //let c = next_lasts[4] - next_lasts[3]).signum(); - if a == dir || a *-1 == dir{ + if a == dir || a * -1 == dir { None } else { next_lasts.pop_back(); @@ -58,14 +56,26 @@ pub fn part2(input: &str) -> String { }) .map(|pos| { let range = pos.0 - pos.1[1]; - let total = if range.x == 0 && range.y > 0 { - (0..range.y).map(|y| pos.0 - IVec2::new(0,y)).map(|v| grid.get(&v).unwrap()).sum::() + let total = if range.x == 0 && range.y > 0 { + (0..range.y) + .map(|y| pos.0 - IVec2::new(0, y)) + .map(|v| grid.get(&v).unwrap()) + .sum::() } else if range.x == 0 && range.y < 0 { - (range.y+1..=0).map(|y| pos.0 - IVec2::new(0,y)).map(|v| grid.get(&v).unwrap()).sum::() + (range.y + 1..=0) + .map(|y| pos.0 - IVec2::new(0, y)) + .map(|v| grid.get(&v).unwrap()) + .sum::() } else if range.y == 0 && range.x > 0 { - (0..range.x).map(|x| pos.0 - IVec2::new(x,0)).map(|v| grid.get(&v).unwrap()).sum::() + (0..range.x) + .map(|x| pos.0 - IVec2::new(x, 0)) + .map(|v| grid.get(&v).unwrap()) + .sum::() } else { - (range.x+1..=0).map(|x| pos.0 - IVec2::new(x,0)).map(|v| grid.get(&v).unwrap()).sum::() + (range.x + 1..=0) + .map(|x| pos.0 - IVec2::new(x, 0)) + .map(|v| grid.get(&v).unwrap()) + .sum::() }; (pos, total) }) @@ -103,7 +113,8 @@ mod test { use rstest::rstest; #[rstest] - #[case("2413432311323 + #[case( + "2413432311323 3215453535623 3255245654254 3446585845452 @@ -115,15 +126,19 @@ mod test { 4564679986453 1224686865563 2546548887735 -4322674655533", "94")] - #[case("111111111111 +4322674655533", + "94" + )] + #[case( + "111111111111 999999999991 999999999991 999999999991 -999999999991", "71")] +999999999991", + "71" + )] fn part2_works(#[case] input: &str, #[case] expected: &str) { let result = part2(input); assert_eq!(result, expected); } } - diff --git a/2023/day-23/src/part1.rs b/2023/day-23/src/part1.rs index a48c077..a0a99c9 100644 --- a/2023/day-23/src/part1.rs +++ b/2023/day-23/src/part1.rs @@ -3,8 +3,7 @@ use std::collections::HashMap; use glam::IVec2; -use petgraph::{prelude::*, algo}; - +use petgraph::{algo, prelude::*}; #[derive(Debug, Copy, Clone)] enum PointType { @@ -32,21 +31,44 @@ pub fn part1(input: &str) -> String { let maze = parse_input(input); //get the start position (assuming there is only one) let start = *maze.keys().find(|pos| pos.y == 0).unwrap(); - let end = maze.keys().fold(IVec2::splat(0), | max, current| if max.y.max(current.y) == current.y { *current } else {max}); - let mut maze_graph = DiGraph::<&PointType, u32>::new(); - let node_map = maze.iter().map(|(pos, point_type)| (pos, maze_graph.add_node(point_type)) ).collect::>(); - - maze.iter().flat_map(|(pos, point_type)| { - point_type.next_possibles().iter().copied().filter_map(|dir| { - let next_pos = dir + *pos; - node_map.get(&next_pos).is_some().then(|| (node_map[pos], node_map[&next_pos], 1)) - }).collect::>() - }) - .for_each(|(a, b, weight)| { - maze_graph.add_edge(a,b,weight); + let end = maze.keys().fold(IVec2::splat(0), |max, current| { + if max.y.max(current.y) == current.y { + *current + } else { + max + } }); + let mut maze_graph = DiGraph::<&PointType, u32>::new(); + let node_map = maze + .iter() + .map(|(pos, point_type)| (pos, maze_graph.add_node(point_type))) + .collect::>(); - (algo::all_simple_paths::,_>(&maze_graph, node_map[&start], node_map[&end], 0, None).max_by(|a, b| a.len().cmp(&b.len())).unwrap().len() -1).to_string() + maze.iter() + .flat_map(|(pos, point_type)| { + point_type + .next_possibles() + .iter() + .copied() + .filter_map(|dir| { + let next_pos = dir + *pos; + node_map + .get(&next_pos) + .is_some() + .then(|| (node_map[pos], node_map[&next_pos], 1)) + }) + .collect::>() + }) + .for_each(|(a, b, weight)| { + maze_graph.add_edge(a, b, weight); + }); + + (algo::all_simple_paths::, _>(&maze_graph, node_map[&start], node_map[&end], 0, None) + .max_by(|a, b| a.len().cmp(&b.len())) + .unwrap() + .len() + - 1) + .to_string() } fn parse_input(input: &str) -> HashMap { @@ -103,5 +125,3 @@ mod test { assert_eq!(result, "94".to_string()); } } - - diff --git a/2023/day-23/src/part2.rs b/2023/day-23/src/part2.rs index db4a3a0..82c1124 100644 --- a/2023/day-23/src/part2.rs +++ b/2023/day-23/src/part2.rs @@ -3,8 +3,7 @@ use std::collections::HashMap; use glam::IVec2; -use petgraph::{prelude::*, algo}; - +use petgraph::{algo, prelude::*}; #[derive(Debug, Copy, Clone)] enum PointType { @@ -28,21 +27,44 @@ pub fn part2(input: &str) -> String { let maze = parse_input(input); //get the start position (assuming there is only one) let start = *maze.keys().find(|pos| pos.y == 0).unwrap(); - let end = maze.keys().fold(IVec2::splat(0), | max, current| if max.y.max(current.y) == current.y { *current } else {max}); - let mut maze_graph = DiGraph::<&PointType, u32>::new(); - let node_map = maze.iter().map(|(pos, point_type)| (pos, maze_graph.add_node(point_type)) ).collect::>(); - - maze.iter().flat_map(|(pos, point_type)| { - point_type.next_possibles().iter().copied().filter_map(|dir| { - let next_pos = dir + *pos; - node_map.get(&next_pos).is_some().then(|| (node_map[pos], node_map[&next_pos], 1)) - }).collect::>() - }) - .for_each(|(a, b, weight)| { - maze_graph.add_edge(a,b,weight); + let end = maze.keys().fold(IVec2::splat(0), |max, current| { + if max.y.max(current.y) == current.y { + *current + } else { + max + } }); + let mut maze_graph = DiGraph::<&PointType, u32>::new(); + let node_map = maze + .iter() + .map(|(pos, point_type)| (pos, maze_graph.add_node(point_type))) + .collect::>(); - (algo::all_simple_paths::,_>(&maze_graph, node_map[&start], node_map[&end], 0, None).max_by(|a, b| a.len().cmp(&b.len())).unwrap().len() -1).to_string() + maze.iter() + .flat_map(|(pos, point_type)| { + point_type + .next_possibles() + .iter() + .copied() + .filter_map(|dir| { + let next_pos = dir + *pos; + node_map + .get(&next_pos) + .is_some() + .then(|| (node_map[pos], node_map[&next_pos], 1)) + }) + .collect::>() + }) + .for_each(|(a, b, weight)| { + maze_graph.add_edge(a, b, weight); + }); + + (algo::all_simple_paths::, _>(&maze_graph, node_map[&start], node_map[&end], 0, None) + .max_by(|a, b| a.len().cmp(&b.len())) + .unwrap() + .len() + - 1) + .to_string() } fn parse_input(input: &str) -> HashMap { @@ -99,5 +121,3 @@ mod test { assert_eq!(result, "154".to_string()); } } - - diff --git a/2023/day-4/src/part1.rs b/2023/day-4/src/part1.rs index 493f7fb..d05d4ed 100644 --- a/2023/day-4/src/part1.rs +++ b/2023/day-4/src/part1.rs @@ -103,7 +103,6 @@ mod test { let (input, card) = parse_card(line).expect("card should be parsed"); assert_eq!(input, ""); assert_eq!(card.get_score(), expected); - } const INPUT: &str = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 diff --git a/2023/day-7/src/part2.rs b/2023/day-7/src/part2.rs index 054a253..61bc168 100644 --- a/2023/day-7/src/part2.rs +++ b/2023/day-7/src/part2.rs @@ -1,12 +1,12 @@ #![warn(clippy::all, clippy::pedantic)] use itertools::Itertools; use nom::{character::complete, multi::separated_list1, sequence::separated_pair, IResult}; +use std::fmt; use std::{ cmp::{Ord, Ordering, PartialOrd}, collections::BTreeMap, str::FromStr, }; -use std::fmt; #[derive(Debug)] struct Day1Part2Error; @@ -70,20 +70,20 @@ impl From<&Card> for &u32 { } impl fmt::Display for Card { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result { - let c = match self { + let c = match self { Card::Joker => 'J', - Card::Two => '2', - Card::Three => '3', - Card::Four => '4', - Card::Five => '5', - Card::Six => '6', - Card::Seven => '7', - Card::Eight => '8', - Card::Nine => '9', - Card::Ten => 'T', - Card::Queen => 'Q', - Card::King => 'K', - Card::Ace => 'A', + Card::Two => '2', + Card::Three => '3', + Card::Four => '4', + Card::Five => '5', + Card::Six => '6', + Card::Seven => '7', + Card::Eight => '8', + Card::Nine => '9', + Card::Ten => 'T', + Card::Queen => 'Q', + Card::King => 'K', + Card::Ace => 'A', }; write!(f, "{c}") } @@ -117,7 +117,7 @@ impl From<&Hand> for HandType { .collect::>()[..] { [(_, x), ..] if jokers + x == 5 => Self::FiveOfAKind, - [] if jokers == 5 => Self::FiveOfAKind, + [] if jokers == 5 => Self::FiveOfAKind, [(_, x), ..] if jokers + x == 4 => Self::FourOfAKind, [(_, 3), (_, 2)] => Self::FullHouse, [(_, 2), (_, 2)] if jokers == 1 => Self::FullHouse, diff --git a/2023/day-9/src/part1.rs b/2023/day-9/src/part1.rs index 9cb03ad..4cdee73 100644 --- a/2023/day-9/src/part1.rs +++ b/2023/day-9/src/part1.rs @@ -59,4 +59,3 @@ mod test { assert_eq!(result, "114".to_string()); } } - diff --git a/2023/day-9/src/part2.rs b/2023/day-9/src/part2.rs index bcb4c33..378b1b8 100644 --- a/2023/day-9/src/part2.rs +++ b/2023/day-9/src/part2.rs @@ -61,4 +61,3 @@ mod test { assert_eq!(result, "2".to_string()); } } -