I took a bit of a different route! @spaciecat killed the simple algorithm answer so I wanted to do something different!
I decided to make a program that lets the user build the path they want to take! Cause why not let the User choose where to explore! I might clean this up and turn it into a full post sometime soon!
Here is a really quick demo GIF
usestd::fmt;usestd::io;usestd::io::prelude::*;fnparse_time_of_walk(user_input:&str)->Result<u32,&'staticstr>{letparsed_input_result:Result<u32,_>=user_input.parse();matchparsed_input_result{Ok(i)=>{ifi%2==0{Ok(i)}else{Err("Walk time must be an even number or we will not be able to get back to where we started")}}Err(_)=>Err("Could not parse the walk time"),}}#[derive(Debug,PartialEq,Clone)]enumDirection{North,South,East,West,}implDirection{fnwill_take_me_home(&self,current_position:(i32,i32))->bool{matchself{Direction::North=>current_position.1<0,Direction::South=>current_position.1>0,Direction::East=>current_position.0<0,Direction::West=>current_position.0>0,}}fnto_str(&self)->&'staticstr{matchself{Direction::North=>"North",Direction::South=>"South",Direction::East=>"East",Direction::West=>"West",}}fndirection_diff(&self)->(i32,i32){matchself{Direction::North=>(0,1),Direction::South=>(0,-1),Direction::East=>(1,0),Direction::West=>(-1,0),}}}implfmt::DisplayforDirection{fnfmt(&self,f:&mutfmt::Formatter)->fmt::Result{matchself{Direction::North=>write!(f,"North"),Direction::South=>write!(f,"South"),Direction::East=>write!(f,"East"),Direction::West=>write!(f,"West"),}}}fnoptions_availible(current_position:(i32,i32),minutes_left:u32)->Option<Vec<Direction>>{ifcurrent_position==(0,0)&&minutes_left==0{returnSome(vec![]);}letdistance_from_origin=current_position.0.abs()+current_position.1.abs();ifdistance_from_origin>minutes_leftasi32{None}else{letall_options:Vec<Direction>=vec![Direction::North,Direction::South,Direction::East,Direction::West,];letcan_take_detour=distance_from_origin<minutes_leftasi32;letoptions=all_options.iter().filter(|direction|direction.will_take_me_home(current_position)||can_take_detour).cloned().collect();Some(options)}}fnchosen_direction(input:&str)->Option<Direction>{letlower=input.to_ascii_lowercase();iflower.starts_with("n"){Some(Direction::North)}elseiflower.starts_with("e"){Some(Direction::East)}elseiflower.starts_with("w"){Some(Direction::West)}elseiflower.starts_with("s"){Some(Direction::South)}else{None}}fndirections_to_string(path:&Vec<Direction>)->String{path.iter().map(Direction::to_str).collect::<Vec<_>>().join(", ")}fnmain()->Result<(),&'staticstr>{println!("Hey I heard you want to take a walk!");println!("How long (in minutes) should we walk for?");letstdin=io::stdin();letmutstdin_lines=stdin.lock().lines();letuser_input_minutes=stdin_lines.next().unwrap().unwrap();letmutminutes_left=parse_time_of_walk(&user_input_minutes)?;letmutcurrent_position=(0,0);letmutpath:Vec<Direction>=vec![];letmutoptions=options_availible(current_position,minutes_left);whileminutes_left>0&&options.is_some(){letunwrapped_options=options.unwrap();println!("\nYou have {} minutes left",minutes_left);println!("Which direction would you like to go?");println!("So far you'r path has been: {}",directions_to_string(&path));println!("Your current options are: {}",directions_to_string(&unwrapped_options));letmutchosen_direction_option=chosen_direction(&stdin_lines.next().unwrap().unwrap());whilechosen_direction_option.is_none(){chosen_direction_option=chosen_direction(&stdin_lines.next().unwrap().unwrap());}letchosen_direction=chosen_direction_option.unwrap();println!("You chose: {}",chosen_direction);current_position.0+=chosen_direction.direction_diff().0;current_position.1+=chosen_direction.direction_diff().1;path.push(chosen_direction);minutes_left-=1;options=options_availible(current_position,minutes_left);}ifminutes_left==0{println!("YAY we made it! Here is the path we took: {}",directions_to_string(&path));}elseifoptions.is_none(){println!("OH NO! We won't be able to make it back in time! Try another path.");println!("Here is what you tried this time: {}",directions_to_string(&path));}Ok(())}#[cfg(test)]modtests{usecrate::*;#[test]fnparse_time_of_walk_test(){assert_eq!(parse_time_of_walk("3"),Err("Walk time must be an even number or we will not be able to get back to where we started"));assert_eq!(parse_time_of_walk("five"),Err("Could not parse the walk time"));assert_eq!(parse_time_of_walk("4"),Ok(4));}#[test]fntest_all_options_availible(){letoutput=options_availible((0,0),10);letall_options=vec![Direction::North,Direction::South,Direction::East,Direction::West,];assert_eq!(output,Some(all_options));}#[test]fntest_impossible_options(){assert_eq!(options_availible((0,-10),1),None);assert_eq!(options_availible((1,10),10),None);assert_eq!(options_availible((-1,10),10),None);}#[test]fntest_already_at_origin_and_no_minutes_left(){assert_eq!(options_availible((0,0),0),Some(vec![]));}#[test]fntest_north_only_option(){letoutput=options_availible((0,-1),1);assert_eq!(output,Some(vec![Direction::North]));}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Barely in before/at midnight!
But here is my Rust version!
I took a bit of a different route! @spaciecat killed the simple algorithm answer so I wanted to do something different!
I decided to make a program that lets the user build the path they want to take! Cause why not let the User choose where to explore! I might clean this up and turn it into a full post sometime soon!
Here is a really quick demo GIF