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;
@@ -26,10 +27,10 @@ enum PipeFrom {
} }
impl PipeFrom { impl PipeFrom {
fn from_ivecs(a: IVec2, b: IVec2) -> Option<Self> { fn from_ivecs(a: IVec2, b: IVec2) -> Option<Self> {
match (a-b).into() { match (a - b).into() {
(0, 1) => Some(Self::Down), (0, 1) => Some(Self::Down),
(0, -1) => Some(Self::Up), (0, -1) => Some(Self::Up),
(1, 0) => Some(Self::Right), (1, 0) => Some(Self::Right),
(-1, 0) => Some(Self::Left), (-1, 0) => Some(Self::Left),
_ => None, _ => None,
//value => unimplemented!("this can't be {a:?} - {b:?} = {value:?}"), //value => unimplemented!("this can't be {a:?} - {b:?} = {value:?}"),
@@ -37,10 +38,10 @@ impl PipeFrom {
} }
fn to_ivec(self) -> IVec2 { fn to_ivec(self) -> IVec2 {
match self { match self {
PipeFrom::Up => (0,-1).into(), PipeFrom::Up => (0, -1).into(),
PipeFrom::Down => (0,1).into(), PipeFrom::Down => (0, 1).into(),
PipeFrom::Left => (-1,0).into(), PipeFrom::Left => (-1, 0).into(),
PipeFrom::Right => (1,0).into(), PipeFrom::Right => (1, 0).into(),
} }
} }
} }
@@ -87,12 +88,12 @@ struct Pipe {
} }
impl Pipe { impl Pipe {
fn get_adjacent(&self ) -> Vec<(IVec2, PipeFrom)> { fn get_adjacent(&self) -> Vec<(IVec2, PipeFrom)> {
self.pipe_type self.pipe_type
.get_adjacents() .get_adjacents()
.into_iter() .into_iter()
.map(|x| x + self.position) .map(|x| x + self.position)
.filter_map(|x| PipeFrom::from_ivecs(self.position , x).map(|y| (x,y) )) .filter_map(|x| PipeFrom::from_ivecs(self.position, x).map(|y| (x, y)))
.collect() .collect()
} }
fn next(&self, from: PipeFrom) -> IVec2 { fn next(&self, from: PipeFrom) -> IVec2 {
@@ -101,10 +102,12 @@ impl Pipe {
match (from, self.pipe_type) { match (from, self.pipe_type) {
(Up, Vertical) | (Left, DownLeft) | (Right, DownRight) => Down, (Up, Vertical) | (Left, DownLeft) | (Right, DownRight) => Down,
(Up, UpLeft) | (Down, DownLeft) | (Right, Horizontal) => Left, (Up, UpLeft) | (Down, DownLeft) | (Right, Horizontal) => Left,
(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
} }
} }
@@ -130,23 +133,31 @@ pub fn part1(input: &str) -> String {
start_node start_node
.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(
.iter() front_nodes
.filter_map(|(pipe, from)| { .iter()
grid.get(&pipe.next(*from)).map(|x|(x,PipeFrom::from_ivecs(pipe.position,x.position ).unwrap())) .filter_map(|(pipe, from)| {
}) grid.get(&pipe.next(*from))
.collect::<Vec<_>>()) .map(|x| (x, PipeFrom::from_ivecs(pipe.position, x.position).unwrap()))
})
.collect::<Vec<_>>(),
)
}, },
) )
.filter(|x| !x.is_empty()) .filter(|x| !x.is_empty())
.position(|a| a[0].0 == a[1].0) .position(|a| a[0].0 == a[1].0)
.unwrap() .unwrap()
+1) + 1)
.to_string() .to_string()
//todo!() //todo!()
} }
@@ -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,18 +84,22 @@ 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!(
Self::Start => "S", f,
Self::Horizontal => "-", "{}",
Self::Vertical => "|", match self {
Self::DownRight => "F", Self::Start => "S",
Self::DownLeft => "7", Self::Horizontal => "-",
Self::UpRight => "L", Self::Vertical => "|",
Self::UpLeft => "J", Self::DownRight => "F",
Self::None => ".", Self::DownLeft => "7",
Self::Outer => "O", Self::UpRight => "L",
Self::Inner => "I", Self::UpLeft => "J",
}) Self::None => ".",
Self::Outer => "O",
Self::Inner => "I",
}
)
} }
} }
@@ -145,24 +149,29 @@ 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()
.filter_map(|(x, from)| grid.get(x).map(|y| (y, *from)))
.filter_map(|(x, from)| {
x.get_adjacent()
.iter() .iter()
.filter_map(|(x, from)| grid.get(x).map(|y| (y, *from))) .map(|(y, _)| y)
.filter_map(|(x, from)| { .contains(&start_node.position)
x.get_adjacent() .then_some(from)
.iter() })
.map(|(y, _)| y) .collect::<Vec<_>>()[..]
.contains(&start_node.position).then_some(from) {
}) [PipeFrom::Up, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Up] => PipeType::DownRight,
.collect::<Vec<_>>()[..]{ [PipeFrom::Up, PipeFrom::Right] | [PipeFrom::Right, PipeFrom::Up] => PipeType::DownLeft,
[PipeFrom::Up, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Up] => PipeType::DownRight, [PipeFrom::Down, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Down] => PipeType::UpRight,
[PipeFrom::Up, PipeFrom::Right] | [PipeFrom::Right, PipeFrom::Up] => PipeType::DownLeft, [PipeFrom::Down, PipeFrom::Right] | [PipeFrom::Right, PipeFrom::Down] => PipeType::UpLeft,
[PipeFrom::Down, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Down] => PipeType::UpRight, [PipeFrom::Up, PipeFrom::Down] | [PipeFrom::Down, PipeFrom::Up] => PipeType::Vertical,
[PipeFrom::Down, PipeFrom::Right] | [PipeFrom::Right, PipeFrom::Down] => PipeType::UpLeft, [PipeFrom::Right, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Right] => {
[PipeFrom::Up, PipeFrom::Down] | [PipeFrom::Down, PipeFrom::Up] => PipeType::Vertical, PipeType::Horizontal
[PipeFrom::Right, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Right] => PipeType::Horizontal, }
_ => PipeType::Start, _ => PipeType::Start,
}; };
let mut pieces = HashMap::new(); let mut pieces = HashMap::new();
pieces.insert(start_node.position, start_node_true_type); pieces.insert(start_node.position, start_node_true_type);
@@ -221,24 +230,23 @@ 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)) .for_each(|pos| {
.for_each(|pos| { if let Some(piece) = pieces.get(&pos) {
if let Some(piece) = pieces.get(&pos) { status = match piece {
status = match piece { PipeType::Vertical | PipeType::DownRight | PipeType::DownLeft => !status,
PipeType::Vertical | PipeType::DownRight | PipeType::DownLeft => !status, _ => status,
_ => status, };
}; } else if status {
} else if status { pieces.insert(pos, PipeType::Inner);
pieces.insert(pos, PipeType::Inner); } else {
} else { pieces.insert(pos, PipeType::Outer);
pieces.insert(pos, PipeType::Outer); }
} });
}); });
});
/* Debug /* Debug
println!(); println!();
(corners.0 .1..=corners.1 .1).for_each(|y| { (corners.0 .1..=corners.1 .1).for_each(|y| {
@@ -365,4 +373,3 @@ L7JLJL-JLJLJL--JLJ.L",
assert_eq!(result, expected); assert_eq!(result, expected);
} }
} }