Pull simulation code out into it's own function
This commit is contained in:
@@ -11,7 +11,7 @@ use std::{
|
|||||||
io::{BufReader, prelude::*},
|
io::{BufReader, prelude::*},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
|
||||||
enum Facing {
|
enum Facing {
|
||||||
Up,
|
Up,
|
||||||
Down,
|
Down,
|
||||||
@@ -54,7 +54,7 @@ impl Position {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
|
||||||
struct Guard {
|
struct Guard {
|
||||||
facing: Facing,
|
facing: Facing,
|
||||||
position: Position,
|
position: Position,
|
||||||
@@ -169,6 +169,33 @@ fn load_map(input_file: &str) -> Result<(Map, Guard), Box<dyn Error>> {
|
|||||||
Ok((Map::new(data), guard_position))
|
Ok((Map::new(data), guard_position))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn simulate_guard_path(mut guard_position: Guard, map: Map) -> (usize, bool) {
|
||||||
|
let mut visited_set: HashSet<Position> = HashSet::new();
|
||||||
|
visited_set.insert(*guard_position.position());
|
||||||
|
let mut guard_states_set: HashSet<Guard> = HashSet::new();
|
||||||
|
|
||||||
|
let mut infinite_loop = false;
|
||||||
|
loop {
|
||||||
|
if guard_states_set.contains(&guard_position) {
|
||||||
|
infinite_loop = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let next_position = guard_position.next_position();
|
||||||
|
match map.get_position(next_position) {
|
||||||
|
Some(true) => {
|
||||||
|
guard_position.turn_clockwise();
|
||||||
|
}
|
||||||
|
Some(false) => {
|
||||||
|
guard_states_set.insert(guard_position);
|
||||||
|
guard_position.advance();
|
||||||
|
visited_set.insert(next_position);
|
||||||
|
}
|
||||||
|
None => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(visited_set.len(), infinite_loop)
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
// Handle command input
|
// Handle command input
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
@@ -179,28 +206,15 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
let input_file = &args[1];
|
let input_file = &args[1];
|
||||||
let (map, mut guard_position) = load_map(input_file)?;
|
let (map, guard_position) = load_map(input_file)?;
|
||||||
|
|
||||||
println!("{}", map);
|
println!("{}", map);
|
||||||
println!("{:?}", guard_position);
|
println!("{:?}", guard_position);
|
||||||
let mut visited_set: HashSet<Position> = HashSet::new();
|
|
||||||
visited_set.insert(*guard_position.position());
|
|
||||||
|
|
||||||
loop {
|
let (locations_visited, infinite_loop) = simulate_guard_path(guard_position, map);
|
||||||
let next_position = guard_position.next_position();
|
|
||||||
match map.get_position(next_position) {
|
|
||||||
Some(true) => {
|
|
||||||
guard_position.turn_clockwise();
|
|
||||||
}
|
|
||||||
Some(false) => {
|
|
||||||
guard_position.advance();
|
|
||||||
visited_set.insert(next_position);
|
|
||||||
}
|
|
||||||
None => break,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("Number of locations visited: {}", visited_set.len());
|
println!("infinite_loop={}", infinite_loop);
|
||||||
|
println!("Number of locations visited: {}", locations_visited);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user