day-10 formated
This commit is contained in:
@@ -8,9 +8,10 @@ use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::tag,
|
||||
character::complete,
|
||||
combinator::eof,
|
||||
multi::{fold_many1, many1},
|
||||
sequence::terminated,
|
||||
IResult, Parser, combinator::eof,
|
||||
IResult, Parser,
|
||||
};
|
||||
use nom_locate::LocatedSpan;
|
||||
|
||||
@@ -26,7 +27,7 @@ enum PipeFrom {
|
||||
}
|
||||
impl PipeFrom {
|
||||
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::Up),
|
||||
(1, 0) => Some(Self::Right),
|
||||
@@ -37,10 +38,10 @@ impl PipeFrom {
|
||||
}
|
||||
fn to_ivec(self) -> IVec2 {
|
||||
match self {
|
||||
PipeFrom::Up => (0,-1).into(),
|
||||
PipeFrom::Down => (0,1).into(),
|
||||
PipeFrom::Left => (-1,0).into(),
|
||||
PipeFrom::Right => (1,0).into(),
|
||||
PipeFrom::Up => (0, -1).into(),
|
||||
PipeFrom::Down => (0, 1).into(),
|
||||
PipeFrom::Left => (-1, 0).into(),
|
||||
PipeFrom::Right => (1, 0).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,12 +88,12 @@ struct Pipe {
|
||||
}
|
||||
|
||||
impl Pipe {
|
||||
fn get_adjacent(&self ) -> Vec<(IVec2, PipeFrom)> {
|
||||
fn get_adjacent(&self) -> Vec<(IVec2, PipeFrom)> {
|
||||
self.pipe_type
|
||||
.get_adjacents()
|
||||
.into_iter()
|
||||
.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()
|
||||
}
|
||||
fn next(&self, from: PipeFrom) -> IVec2 {
|
||||
@@ -104,7 +105,9 @@ impl Pipe {
|
||||
(Up, UpRight) | (Down, DownRight) | (Left, Horizontal) => Right,
|
||||
(Down, Vertical) | (Left, UpLeft) | (Right, UpRight) => Up,
|
||||
_ => unimplemented!("no"),
|
||||
}.to_ivec() + self.position
|
||||
}
|
||||
.to_ivec()
|
||||
+ self.position
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,23 +133,31 @@ pub fn part1(input: &str) -> String {
|
||||
start_node
|
||||
.get_adjacent()
|
||||
.iter()
|
||||
.filter_map(|(x, from)| grid.get(x).map(|y|(y,*from)))
|
||||
.filter(|(x, _)| x.get_adjacent().iter().map(|(y,_)|y).contains(&start_node.position))
|
||||
.collect::<Vec<_>>()
|
||||
.filter_map(|(x, from)| grid.get(x).map(|y| (y, *from)))
|
||||
.filter(|(x, _)| {
|
||||
x.get_adjacent()
|
||||
.iter()
|
||||
.map(|(y, _)| y)
|
||||
.contains(&start_node.position)
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
),
|
||||
|front_nodes| {
|
||||
Some(front_nodes
|
||||
Some(
|
||||
front_nodes
|
||||
.iter()
|
||||
.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())
|
||||
.position(|a| a[0].0 == a[1].0)
|
||||
.unwrap()
|
||||
+1)
|
||||
+ 1)
|
||||
.to_string()
|
||||
//todo!()
|
||||
}
|
||||
@@ -254,4 +265,3 @@ LJ.LJ",
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#![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 itertools::Itertools;
|
||||
@@ -84,7 +84,10 @@ impl PipeType {
|
||||
}
|
||||
impl Display for PipeType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", match self {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
Self::Start => "S",
|
||||
Self::Horizontal => "-",
|
||||
Self::Vertical => "|",
|
||||
@@ -95,7 +98,8 @@ impl Display for PipeType {
|
||||
Self::None => ".",
|
||||
Self::Outer => "O",
|
||||
Self::Inner => "I",
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,22 +149,27 @@ pub fn part2(input: &str) -> String {
|
||||
.values()
|
||||
.find(|x| x.pipe_type == PipeType::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()
|
||||
.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::Right] | [PipeFrom::Right, PipeFrom::Up] => PipeType::DownLeft,
|
||||
[PipeFrom::Down, PipeFrom::Left] | [PipeFrom::Left, PipeFrom::Down] => PipeType::UpRight,
|
||||
[PipeFrom::Down, PipeFrom::Right] | [PipeFrom::Right, PipeFrom::Down] => PipeType::UpLeft,
|
||||
[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,
|
||||
};
|
||||
|
||||
@@ -221,10 +230,9 @@ pub fn part2(input: &str) -> String {
|
||||
print!("\n");
|
||||
});
|
||||
*/
|
||||
(corners.0.1..=corners.1.1)
|
||||
.for_each(|y| {
|
||||
(corners.0 .1..=corners.1 .1).for_each(|y| {
|
||||
let mut status = false;
|
||||
(corners.0.0..=corners.1 .0)
|
||||
(corners.0 .0..=corners.1 .0)
|
||||
.map(|x| IVec2::new(x, y))
|
||||
.for_each(|pos| {
|
||||
if let Some(piece) = pieces.get(&pos) {
|
||||
@@ -365,4 +373,3 @@ L7JLJL-JLJLJL--JLJ.L",
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user