rustfmt 2023

This commit is contained in:
Dylan Thies
2023-12-29 08:57:56 -05:00
parent c05ab694ab
commit 49009dfada
11 changed files with 133 additions and 79 deletions

View File

@@ -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::<u32>()
.to_string(),
))

View File

@@ -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::<u32>()
.to_string()
}
@@ -42,7 +45,11 @@ fn parse_line(line: &str) -> Vec<u32> {
} 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

View File

@@ -11,12 +11,7 @@
pub fn part1(input: &str) -> String {
input
.lines()
.map(|x| {
x.split(',')
.map(unhash)
.sum::<usize>()
.to_string()
})
.map(|x| x.split(',').map(unhash).sum::<usize>().to_string())
.next()
.unwrap()
}

View File

@@ -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());
}
}

View File

@@ -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;
@@ -59,13 +57,25 @@ 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::<u32>()
(0..range.y)
.map(|y| pos.0 - IVec2::new(0, y))
.map(|v| grid.get(&v).unwrap())
.sum::<u32>()
} 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::<u32>()
(range.y + 1..=0)
.map(|y| pos.0 - IVec2::new(0, y))
.map(|v| grid.get(&v).unwrap())
.sum::<u32>()
} 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::<u32>()
(0..range.x)
.map(|x| pos.0 - IVec2::new(x, 0))
.map(|v| grid.get(&v).unwrap())
.sum::<u32>()
} else {
(range.x+1..=0).map(|x| pos.0 - IVec2::new(x,0)).map(|v| grid.get(&v).unwrap()).sum::<u32>()
(range.x + 1..=0)
.map(|x| pos.0 - IVec2::new(x, 0))
.map(|v| grid.get(&v).unwrap())
.sum::<u32>()
};
(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);
}
}

View File

@@ -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 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::<HashMap<_,_>>();
let node_map = maze
.iter()
.map(|(pos, point_type)| (pos, maze_graph.add_node(point_type)))
.collect::<HashMap<_, _>>();
maze.iter().flat_map(|(pos, point_type)| {
point_type.next_possibles().iter().copied().filter_map(|dir| {
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::<Vec<_>>()
node_map
.get(&next_pos)
.is_some()
.then(|| (node_map[pos], node_map[&next_pos], 1))
})
.collect::<Vec<_>>()
})
.for_each(|(a, b, weight)| {
maze_graph.add_edge(a, b, weight);
});
(algo::all_simple_paths::<Vec<_>,_>(&maze_graph, node_map[&start], node_map[&end], 0, None).max_by(|a, b| a.len().cmp(&b.len())).unwrap().len() -1).to_string()
(algo::all_simple_paths::<Vec<_>, _>(&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<IVec2, PointType> {
@@ -103,5 +125,3 @@ mod test {
assert_eq!(result, "94".to_string());
}
}

View File

@@ -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 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::<HashMap<_,_>>();
let node_map = maze
.iter()
.map(|(pos, point_type)| (pos, maze_graph.add_node(point_type)))
.collect::<HashMap<_, _>>();
maze.iter().flat_map(|(pos, point_type)| {
point_type.next_possibles().iter().copied().filter_map(|dir| {
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::<Vec<_>>()
node_map
.get(&next_pos)
.is_some()
.then(|| (node_map[pos], node_map[&next_pos], 1))
})
.collect::<Vec<_>>()
})
.for_each(|(a, b, weight)| {
maze_graph.add_edge(a, b, weight);
});
(algo::all_simple_paths::<Vec<_>,_>(&maze_graph, node_map[&start], node_map[&end], 0, None).max_by(|a, b| a.len().cmp(&b.len())).unwrap().len() -1).to_string()
(algo::all_simple_paths::<Vec<_>, _>(&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<IVec2, PointType> {
@@ -99,5 +121,3 @@ mod test {
assert_eq!(result, "154".to_string());
}
}

View File

@@ -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

View File

@@ -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;

View File

@@ -59,4 +59,3 @@ mod test {
assert_eq!(result, "114".to_string());
}
}

View File

@@ -61,4 +61,3 @@ mod test {
assert_eq!(result, "2".to_string());
}
}