day-10 formated

This commit is contained in:
Dylan Thies
2023-12-12 08:45:47 -05:00
parent 2e97d7045c
commit e9701055e0
2 changed files with 91 additions and 74 deletions

View File

@@ -8,9 +8,10 @@ use nom::{
branch::alt, branch::alt,
bytes::complete::tag, bytes::complete::tag,
character::complete, character::complete,
combinator::eof,
multi::{fold_many1, many1}, multi::{fold_many1, many1},
sequence::terminated, sequence::terminated,
IResult, Parser, combinator::eof, IResult, Parser,
}; };
use nom_locate::LocatedSpan; use nom_locate::LocatedSpan;
@@ -104,7 +105,9 @@ impl Pipe {
(Up, UpRight) | (Down, DownRight) | (Left, Horizontal) => Right, (Up, UpRight) | (Down, DownRight) | (Left, Horizontal) => Right,
(Down, Vertical) | (Left, UpLeft) | (Right, UpRight) => Up, (Down, Vertical) | (Left, UpLeft) | (Right, UpRight) => Up,
_ => unimplemented!("no"), _ => unimplemented!("no"),
}.to_ivec() + self.position }
.to_ivec()
+ self.position
} }
} }
@@ -131,16 +134,24 @@ pub fn part1(input: &str) -> String {
.get_adjacent() .get_adjacent()
.iter() .iter()
.filter_map(|(x, from)| grid.get(x).map(|y| (y, *from))) .filter_map(|(x, from)| grid.get(x).map(|y| (y, *from)))
.filter(|(x, _)| x.get_adjacent().iter().map(|(y,_)|y).contains(&start_node.position)) .filter(|(x, _)| {
.collect::<Vec<_>>() x.get_adjacent()
.iter()
.map(|(y, _)| y)
.contains(&start_node.position)
})
.collect::<Vec<_>>(),
), ),
|front_nodes| { |front_nodes| {
Some(front_nodes Some(
front_nodes
.iter() .iter()
.filter_map(|(pipe, from)| { .filter_map(|(pipe, from)| {
grid.get(&pipe.next(*from)).map(|x|(x,PipeFrom::from_ivecs(pipe.position,x.position ).unwrap())) grid.get(&pipe.next(*from))
.map(|x| (x, PipeFrom::from_ivecs(pipe.position, x.position).unwrap()))
}) })
.collect::<Vec<_>>()) .collect::<Vec<_>>(),
)
}, },
) )
.filter(|x| !x.is_empty()) .filter(|x| !x.is_empty())
@@ -254,4 +265,3 @@ LJ.LJ",
assert_eq!(result, expected); assert_eq!(result, expected);
} }
} }

View File

@@ -1,6 +1,6 @@
#![warn(clippy::all, clippy::pedantic)] #![warn(clippy::all, clippy::pedantic)]
use std::{collections::HashMap, iter::successors, fmt::Display}; use std::{collections::HashMap, fmt::Display, iter::successors};
use glam::IVec2; use glam::IVec2;
use itertools::Itertools; use itertools::Itertools;
@@ -84,7 +84,10 @@ impl PipeType {
} }
impl Display for PipeType { impl Display for PipeType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self { write!(
f,
"{}",
match self {
Self::Start => "S", Self::Start => "S",
Self::Horizontal => "-", Self::Horizontal => "-",
Self::Vertical => "|", Self::Vertical => "|",
@@ -95,7 +98,8 @@ impl Display for PipeType {
Self::None => ".", Self::None => ".",
Self::Outer => "O", Self::Outer => "O",
Self::Inner => "I", Self::Inner => "I",
}) }
)
} }
} }
@@ -145,22 +149,27 @@ pub fn part2(input: &str) -> String {
.values() .values()
.find(|x| x.pipe_type == PipeType::Start) .find(|x| x.pipe_type == PipeType::Start)
.expect("has a start"); .expect("has a start");
let start_node_true_type = match &start_node.get_adjacent() let start_node_true_type = match &start_node
.get_adjacent()
.iter() .iter()
.filter_map(|(x, from)| grid.get(x).map(|y| (y, *from))) .filter_map(|(x, from)| grid.get(x).map(|y| (y, *from)))
.filter_map(|(x, from)| { .filter_map(|(x, from)| {
x.get_adjacent() x.get_adjacent()
.iter() .iter()
.map(|(y, _)| y) .map(|(y, _)| y)
.contains(&start_node.position).then_some(from) .contains(&start_node.position)
.then_some(from)
}) })
.collect::<Vec<_>>()[..]{ .collect::<Vec<_>>()[..]
{
[PipeFrom::Up, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Up] => PipeType::DownRight, [PipeFrom::Up, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Up] => PipeType::DownRight,
[PipeFrom::Up, PipeFrom::Right] | [PipeFrom::Right, PipeFrom::Up] => PipeType::DownLeft, [PipeFrom::Up, PipeFrom::Right] | [PipeFrom::Right, PipeFrom::Up] => PipeType::DownLeft,
[PipeFrom::Down, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Down] => PipeType::UpRight, [PipeFrom::Down, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Down] => PipeType::UpRight,
[PipeFrom::Down, PipeFrom::Right] | [PipeFrom::Right, PipeFrom::Down] => PipeType::UpLeft, [PipeFrom::Down, PipeFrom::Right] | [PipeFrom::Right, PipeFrom::Down] => PipeType::UpLeft,
[PipeFrom::Up, PipeFrom::Down] | [PipeFrom::Down, PipeFrom::Up] => PipeType::Vertical, [PipeFrom::Up, PipeFrom::Down] | [PipeFrom::Down, PipeFrom::Up] => PipeType::Vertical,
[PipeFrom::Right, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Right] => PipeType::Horizontal, [PipeFrom::Right, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Right] => {
PipeType::Horizontal
}
_ => PipeType::Start, _ => PipeType::Start,
}; };
@@ -221,8 +230,7 @@ pub fn part2(input: &str) -> String {
print!("\n"); print!("\n");
}); });
*/ */
(corners.0.1..=corners.1.1) (corners.0 .1..=corners.1 .1).for_each(|y| {
.for_each(|y| {
let mut status = false; let mut status = false;
(corners.0 .0..=corners.1 .0) (corners.0 .0..=corners.1 .0)
.map(|x| IVec2::new(x, y)) .map(|x| IVec2::new(x, y))
@@ -365,4 +373,3 @@ L7JLJL-JLJLJL--JLJ.L",
assert_eq!(result, expected); assert_eq!(result, expected);
} }
} }