2025-05-31 08:50:55 -05:00
|
|
|
use std::{
|
|
|
|
|
env,
|
|
|
|
|
error::Error,
|
|
|
|
|
fs::File,
|
|
|
|
|
io::{BufReader, prelude::*},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fn load_map(input_file: &str) -> Result<(Vec<Vec<bool>>, (usize, usize)), Box<dyn Error>> {
|
|
|
|
|
let buffer = BufReader::new(File::open(input_file)?);
|
|
|
|
|
let mut lines = buffer.lines().peekable();
|
|
|
|
|
|
|
|
|
|
let width = match lines.peek() {
|
|
|
|
|
Some(l) => match l {
|
|
|
|
|
Ok(li) => li.len(),
|
|
|
|
|
Err(_) => Err("Error reading first line of map file")?,
|
|
|
|
|
},
|
|
|
|
|
None => Err("Cannot load empty map file.")?,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut data: Vec<Vec<bool>> = Vec::new();
|
|
|
|
|
let mut guard_position = (0, 0);
|
|
|
|
|
|
|
|
|
|
for (y, line) in lines.enumerate() {
|
|
|
|
|
let line = line?;
|
|
|
|
|
if line.len() != width {
|
|
|
|
|
println!("{}", line);
|
|
|
|
|
Err("Line length of input are not consistent")?;
|
|
|
|
|
}
|
|
|
|
|
let parsed_data: Vec<bool> = line
|
|
|
|
|
.chars()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.map(|(x, c)| {
|
|
|
|
|
if c == '^' {
|
|
|
|
|
guard_position = (x, y);
|
|
|
|
|
};
|
|
|
|
|
c == '#'
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
data.push(parsed_data);
|
|
|
|
|
}
|
|
|
|
|
let height = data.len();
|
|
|
|
|
|
|
|
|
|
Ok((data, guard_position))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
|
// Handle command input
|
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
|
if args.len() != 2 {
|
|
|
|
|
panic!(
|
|
|
|
|
"{} must be run with a single argument of files name!",
|
|
|
|
|
&args[0]
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
let input_file = &args[1];
|
|
|
|
|
let data = load_map(input_file)?;
|
|
|
|
|
println!("{:?}", data);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-05-30 14:35:27 -05:00
|
|
|
}
|