day-13 done with clippy and format
This commit is contained in:
@@ -4,10 +4,7 @@ use std::collections::HashSet;
|
|||||||
|
|
||||||
use glam::UVec2;
|
use glam::UVec2;
|
||||||
use nom::{
|
use nom::{
|
||||||
bytes::complete::is_a,
|
bytes::complete::is_a, character::complete, multi::separated_list1, sequence::tuple, IResult,
|
||||||
character::complete,
|
|
||||||
multi::separated_list1,
|
|
||||||
IResult, sequence::tuple,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Drawing {
|
struct Drawing {
|
||||||
@@ -19,39 +16,39 @@ impl Drawing {
|
|||||||
fn process(&self) -> u32 {
|
fn process(&self) -> u32 {
|
||||||
let (max_col, max_row) = self.size.into();
|
let (max_col, max_row) = self.size.into();
|
||||||
let col_score = (1..max_col)
|
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);
|
let span = reflect_col.min(max_col - reflect_col);
|
||||||
(self.mounds
|
self
|
||||||
|
.mounds
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|mound| mound.x + span >= reflect_col && mound.x < reflect_col)
|
.filter(|mound| mound.x + span >= reflect_col && mound.x < reflect_col)
|
||||||
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
|
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
|
||||||
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
|
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
|
||||||
&&
|
&& self
|
||||||
self.mounds
|
.mounds
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|mound| mound.x < reflect_col + span && mound.x >= reflect_col)
|
.filter(|mound| mound.x < reflect_col + span && mound.x >= reflect_col)
|
||||||
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
|
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
|
||||||
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
|
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
|
||||||
)
|
|
||||||
.then_some(reflect_col)
|
|
||||||
})
|
})
|
||||||
.sum::<u32>();
|
.sum::<u32>();
|
||||||
let row_score = (1..max_row)
|
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);
|
let span = reflect_row.min(max_row - reflect_row);
|
||||||
(self.mounds
|
self
|
||||||
|
.mounds
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|mound| mound.y + span >= reflect_row && mound.y < reflect_row)
|
.filter(|mound| mound.y + span >= reflect_row && mound.y < reflect_row)
|
||||||
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
|
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
|
||||||
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
|
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
|
||||||
&&
|
&& self
|
||||||
self.mounds
|
.mounds
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|mound| mound.y < reflect_row + span && mound.y >= reflect_row)
|
.filter(|mound| mound.y < reflect_row + span && mound.y >= reflect_row)
|
||||||
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
|
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
|
||||||
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
|
.all(|mound_reflect| self.mounds.get(&mound_reflect).is_some())
|
||||||
)
|
|
||||||
.then_some(reflect_row)
|
|
||||||
})
|
})
|
||||||
.sum::<u32>()
|
.sum::<u32>()
|
||||||
* 100;
|
* 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]
|
#[must_use]
|
||||||
pub fn part1(_input: &str) -> String {
|
pub fn part1(input: &str) -> String {
|
||||||
let (_, drawings) = parse_input(_input).expect("aoc always valid");
|
let (_, drawings) = parse_input(input).expect("aoc always valid");
|
||||||
drawings.iter().map(Drawing::process).sum::<u32>().to_string()
|
drawings
|
||||||
|
.iter()
|
||||||
|
.map(Drawing::process)
|
||||||
|
.sum::<u32>()
|
||||||
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_drawing(input: &str) -> IResult<&str, Drawing> {
|
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>> {
|
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)]
|
#[cfg(test)]
|
||||||
@@ -144,4 +156,3 @@ mod test {
|
|||||||
assert_eq!(result, "405".to_string());
|
assert_eq!(result, "405".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,7 @@ use std::collections::HashSet;
|
|||||||
|
|
||||||
use glam::UVec2;
|
use glam::UVec2;
|
||||||
use nom::{
|
use nom::{
|
||||||
bytes::complete::is_a,
|
bytes::complete::is_a, character::complete, multi::separated_list1, sequence::tuple, IResult,
|
||||||
character::complete,
|
|
||||||
multi::separated_list1,
|
|
||||||
IResult, sequence::tuple,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Drawing {
|
struct Drawing {
|
||||||
@@ -19,43 +16,45 @@ impl Drawing {
|
|||||||
fn process(&self) -> u32 {
|
fn process(&self) -> u32 {
|
||||||
let (max_col, max_row) = self.size.into();
|
let (max_col, max_row) = self.size.into();
|
||||||
let col_score = (1..max_col)
|
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);
|
let span = reflect_col.min(max_col - reflect_col);
|
||||||
((self.mounds
|
(self
|
||||||
|
.mounds
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|mound| mound.x + span >= reflect_col && mound.x < reflect_col)
|
.filter(|mound| mound.x + span >= reflect_col && mound.x < reflect_col)
|
||||||
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
|
.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()
|
.count()
|
||||||
+
|
+ self
|
||||||
self.mounds
|
.mounds
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|mound| mound.x < reflect_col + span && mound.x >= reflect_col)
|
.filter(|mound| mound.x < reflect_col + span && mound.x >= reflect_col)
|
||||||
.map(|mound| (2 * reflect_col - mound.x - 1, mound.y).into())
|
.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()
|
.count())
|
||||||
) == 1)
|
== 1
|
||||||
.then_some(reflect_col)
|
|
||||||
})
|
})
|
||||||
.sum::<u32>();
|
.sum::<u32>();
|
||||||
let row_score = (1..max_row)
|
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);
|
let span = reflect_row.min(max_row - reflect_row);
|
||||||
((self.mounds
|
(self
|
||||||
|
.mounds
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|mound| mound.y + span >= reflect_row && mound.y < reflect_row)
|
.filter(|mound| mound.y + span >= reflect_row && mound.y < reflect_row)
|
||||||
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
|
.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()
|
.count()
|
||||||
+
|
+ self
|
||||||
self.mounds
|
.mounds
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|mound| mound.y < reflect_row + span && mound.y >= reflect_row)
|
.filter(|mound| mound.y < reflect_row + span && mound.y >= reflect_row)
|
||||||
.map(|mound| (mound.x, 2 * reflect_row - mound.y - 1).into())
|
.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()
|
.count())
|
||||||
) == 1 )
|
== 1
|
||||||
.then_some(reflect_row)
|
|
||||||
})
|
})
|
||||||
.sum::<u32>()
|
.sum::<u32>()
|
||||||
* 100;
|
* 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]
|
#[must_use]
|
||||||
pub fn part2(_input: &str) -> String {
|
pub fn part2(input: &str) -> String {
|
||||||
let (_, drawings) = parse_input(_input).expect("aoc always valid");
|
let (_, drawings) = parse_input(input).expect("aoc always valid");
|
||||||
drawings.iter().map(Drawing::process).sum::<u32>().to_string()
|
drawings
|
||||||
|
.iter()
|
||||||
|
.map(Drawing::process)
|
||||||
|
.sum::<u32>()
|
||||||
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_drawing(input: &str) -> IResult<&str, Drawing> {
|
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>> {
|
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)]
|
#[cfg(test)]
|
||||||
@@ -148,4 +162,3 @@ mod test {
|
|||||||
assert_eq!(result, "400".to_string());
|
assert_eq!(result, "400".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user