doing some clippy and testing gittea
This commit is contained in:
@@ -63,7 +63,7 @@ struct MyMap {
|
||||
}
|
||||
|
||||
impl MyMap {
|
||||
pub fn next_obstacle(&self, start_pos: &IVec2, direction: &Direction) -> Option<&IVec2> {
|
||||
pub fn next_obstacle(&self, start_pos: IVec2, direction: &Direction) -> Option<&IVec2> {
|
||||
self.obstacles
|
||||
.iter()
|
||||
.filter(|obstacle| match direction {
|
||||
@@ -73,10 +73,10 @@ impl MyMap {
|
||||
Direction::West => obstacle.x == start_pos.x && obstacle.y < start_pos.y,
|
||||
})
|
||||
.fold(None, |acc, obstacle| match direction {
|
||||
Direction::North if acc == None || obstacle.x > acc.unwrap().x => Some(obstacle),
|
||||
Direction::East if acc == None || obstacle.y < acc.unwrap().y => Some(obstacle),
|
||||
Direction::South if acc == None || obstacle.x < acc.unwrap().x => Some(obstacle),
|
||||
Direction::West if acc == None || obstacle.y > acc.unwrap().y => Some(obstacle),
|
||||
Direction::North if acc.is_none() || obstacle.x > acc.unwrap().x => Some(obstacle),
|
||||
Direction::East if acc.is_none() || obstacle.y < acc.unwrap().y => Some(obstacle),
|
||||
Direction::South if acc.is_none() || obstacle.x < acc.unwrap().x => Some(obstacle),
|
||||
Direction::West if acc.is_none() || obstacle.y > acc.unwrap().y => Some(obstacle),
|
||||
_ => acc,
|
||||
})
|
||||
}
|
||||
@@ -87,6 +87,9 @@ impl MyMap {
|
||||
///
|
||||
/// # Errors
|
||||
/// - `ParseError` there was an issue with the parser
|
||||
///
|
||||
/// # Panics
|
||||
/// - it just does
|
||||
pub fn part1(input: &str) -> Result<String, Day6Part1Error> {
|
||||
//let input = Span::new(input);
|
||||
//TODO figure out how to real error
|
||||
@@ -99,7 +102,7 @@ pub fn part1(input: &str) -> Result<String, Day6Part1Error> {
|
||||
&& (guard_pos.y as u32) < map.width
|
||||
{
|
||||
let _ = visited.insert(guard_pos);
|
||||
if let Some(next_obstacle) = map.next_obstacle(&guard_pos, &guard_dir) {
|
||||
if let Some(next_obstacle) = map.next_obstacle(guard_pos, &guard_dir) {
|
||||
// println!("Hit row {}, col {} going {:?}", next_obstacle.x, next_obstacle.y, guard_dir);
|
||||
match guard_dir {
|
||||
Direction::North => {
|
||||
@@ -130,7 +133,7 @@ pub fn part1(input: &str) -> Result<String, Day6Part1Error> {
|
||||
visited.insert(x);
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
guard_pos = next_obstacle - &guard_dir;
|
||||
} else {
|
||||
let new_pos = match guard_dir {
|
||||
@@ -169,7 +172,7 @@ pub fn part1(input: &str) -> Result<String, Day6Part1Error> {
|
||||
visited.insert(x);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
guard_pos = new_pos;
|
||||
//break
|
||||
}
|
||||
@@ -187,12 +190,25 @@ pub fn part1(input: &str) -> Result<String, Day6Part1Error> {
|
||||
print!("\n");
|
||||
}*/
|
||||
}
|
||||
for row in 0.. map.height.try_into().unwrap() {
|
||||
for col in 0..map.width.try_into().unwrap() {
|
||||
let pos = IVec2::new(row, col);
|
||||
if visited.contains(&pos) {
|
||||
print!("X");
|
||||
}else if map.obstacles.contains(&pos) {
|
||||
print!("#");
|
||||
} else {
|
||||
print!(".");
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
Ok(visited.iter().count().to_string())
|
||||
Ok(visited.len().to_string())
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> (IVec2, MyMap) {
|
||||
let (pos, height, width, obstacles) = input.lines().into_iter().enumerate().fold(
|
||||
let (pos, height, width, obstacles) = input.lines().enumerate().fold(
|
||||
(IVec2::ZERO, 0, 0, HashSet::new()),
|
||||
|mut acc, (row_no, row)| {
|
||||
acc.1 = row_no.try_into().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user