day-13 done with clippy and format

This commit is contained in:
Dylan Thies
2023-12-13 23:14:18 -05:00
parent 3283c0b692
commit 12ffb7f6bf
2 changed files with 96 additions and 72 deletions

View File

@@ -4,10 +4,7 @@ use std::collections::HashSet;
use glam::UVec2;
use nom::{
bytes::complete::is_a,
character::complete,
multi::separated_list1,
IResult, sequence::tuple,
bytes::complete::is_a, character::complete, multi::separated_list1, sequence::tuple, IResult,
};
struct Drawing {
@@ -19,39 +16,39 @@ impl Drawing {
fn process(&self) -> u32 {
let (max_col, max_row) = self.size.into();
let col_score = (1..max_col)
.filter_map(|reflect_col| {
.filter(|reflect_col| {
let reflect_col = * reflect_col;
let span = reflect_col.min(max_col - reflect_col);
(self.mounds
self
.mounds
.iter()
.filter(|mound| mound.x + span >= reflect_col && mound.x < reflect_col)
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
&&
self.mounds
&& self
.mounds
.iter()
.filter(|mound| mound.x < reflect_col + span && mound.x >= reflect_col)
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
)
.then_some(reflect_col)
})
.sum::<u32>();
let row_score = (1..max_row)
.filter_map(|reflect_row| {
.filter(|reflect_row| {
let reflect_row = * reflect_row;
let span = reflect_row.min(max_row - reflect_row);
(self.mounds
self
.mounds
.iter()
.filter(|mound| mound.y + span >= reflect_row && mound.y < reflect_row)
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
&&
self.mounds
&& self
.mounds
.iter()
.filter(|mound| mound.y < reflect_row + span && mound.y >= reflect_row)
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
)
.then_some(reflect_row)
})
.sum::<u32>()
* 100;
@@ -59,10 +56,22 @@ impl Drawing {
}
}
/// day 13 part 1 of aoc 2023
///
/// # Arguments
/// - input the input for today's puzzle
///
/// # Panics
/// panics whne it cannot parse the input OR when ever the number of game numbers is greater than
/// usize
#[must_use]
pub fn part1(_input: &str) -> String {
let (_, drawings) = parse_input(_input).expect("aoc always valid");
drawings.iter().map(Drawing::process).sum::<u32>().to_string()
pub fn part1(input: &str) -> String {
let (_, drawings) = parse_input(input).expect("aoc always valid");
drawings
.iter()
.map(Drawing::process)
.sum::<u32>()
.to_string()
}
fn parse_drawing(input: &str) -> IResult<&str, Drawing> {
@@ -88,7 +97,10 @@ fn parse_drawing(input: &str) -> IResult<&str, Drawing> {
}
fn parse_input(input: &str) -> IResult<&str, Vec<Drawing>> {
separated_list1(tuple((complete::line_ending, complete::line_ending)), parse_drawing)(input)
separated_list1(
tuple((complete::line_ending, complete::line_ending)),
parse_drawing,
)(input)
}
#[cfg(test)]
@@ -144,4 +156,3 @@ mod test {
assert_eq!(result, "405".to_string());
}
}

View File

@@ -4,10 +4,7 @@ use std::collections::HashSet;
use glam::UVec2;
use nom::{
bytes::complete::is_a,
character::complete,
multi::separated_list1,
IResult, sequence::tuple,
bytes::complete::is_a, character::complete, multi::separated_list1, sequence::tuple, IResult,
};
struct Drawing {
@@ -19,43 +16,45 @@ impl Drawing {
fn process(&self) -> u32 {
let (max_col, max_row) = self.size.into();
let col_score = (1..max_col)
.filter_map(|reflect_col| {
.filter(|reflect_col| {
let reflect_col = *reflect_col;
let span = reflect_col.min(max_col - reflect_col);
((self.mounds
(self
.mounds
.iter()
.filter(|mound| mound.x + span >= reflect_col && mound.x < reflect_col)
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
.filter(|mound_reflect| self.mounds.get(&mound_reflect).is_none())
.filter(|mound_reflect| self.mounds.get(mound_reflect).is_none())
.count()
+
self.mounds
+ self
.mounds
.iter()
.filter(|mound| mound.x < reflect_col + span && mound.x >= reflect_col)
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
.filter(|mound_reflect| self.mounds.get(&mound_reflect).is_none())
.count()
) == 1)
.then_some(reflect_col)
.filter(|mound_reflect| self.mounds.get(mound_reflect).is_none())
.count())
== 1
})
.sum::<u32>();
let row_score = (1..max_row)
.filter_map(|reflect_row| {
.filter(|reflect_row| {
let reflect_row = *reflect_row;
let span = reflect_row.min(max_row - reflect_row);
((self.mounds
(self
.mounds
.iter()
.filter(|mound| mound.y + span >= reflect_row && mound.y < reflect_row)
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
.filter(|mound_reflect| self.mounds.get(&mound_reflect).is_none())
.filter(|mound_reflect| self.mounds.get(mound_reflect).is_none())
.count()
+
self.mounds
+ self
.mounds
.iter()
.filter(|mound| mound.y < reflect_row + span && mound.y >= reflect_row)
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
.filter(|mound_reflect| self.mounds.get(&mound_reflect).is_none())
.count()
) == 1 )
.then_some(reflect_row)
.filter(|mound_reflect| self.mounds.get(mound_reflect).is_none())
.count())
== 1
})
.sum::<u32>()
* 100;
@@ -63,10 +62,22 @@ impl Drawing {
}
}
/// day 13 part 1 of aoc 2023
///
/// # Arguments
/// - input the input for today's puzzle
///
/// # Panics
/// panics whne it cannot parse the input OR when ever the number of game numbers is greater than
/// usize
#[must_use]
pub fn part2(_input: &str) -> String {
let (_, drawings) = parse_input(_input).expect("aoc always valid");
drawings.iter().map(Drawing::process).sum::<u32>().to_string()
pub fn part2(input: &str) -> String {
let (_, drawings) = parse_input(input).expect("aoc always valid");
drawings
.iter()
.map(Drawing::process)
.sum::<u32>()
.to_string()
}
fn parse_drawing(input: &str) -> IResult<&str, Drawing> {
@@ -92,7 +103,10 @@ fn parse_drawing(input: &str) -> IResult<&str, Drawing> {
}
fn parse_input(input: &str) -> IResult<&str, Vec<Drawing>> {
separated_list1(tuple((complete::line_ending, complete::line_ending)), parse_drawing)(input)
separated_list1(
tuple((complete::line_ending, complete::line_ending)),
parse_drawing,
)(input)
}
#[cfg(test)]
@@ -148,4 +162,3 @@ mod test {
assert_eq!(result, "400".to_string());
}
}