use std::{ env, error::Error, fs::File, io::{BufReader, prelude::*}, }; fn load_map(input_file: &str) -> Result<(Vec>, (usize, usize)), Box> { 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::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 = 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> { // Handle command input let args: Vec = 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(()) }