Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Not too bad. Follow the steps: Input -> Process -> Output -> Repeat and keep track of the touched squares in a hashmap. Could have gone full-hog and made structs out of points and headings, but this makes sorting easier. Here's my Rust solution:
/// Day 11: Space Police/// /// Repaint your ship to comply with the fuzzusestd::collections::HashMap;usestd::fs;usecrate::intcode::IntcodeInterpreter;#[derive(Eq,PartialEq,PartialOrd,Ord,Debug)]enumColor{White,Black,}/// A Position is a ordered pair (x, y) in 2D space.typePosition=(isize,isize);/// A Heading is a direction vector (x, y) with y positive being up.typeHeading=(isize,isize);/// Parses the input. Expects a single line of integers separated by/// commasfnparse_input()->Vec<isize>{lettext:String=fs::read_to_string("data/day11.txt").unwrap();letcleaned=text.trim();cleaned.split(",").map(|c|c.parse().unwrap()).collect()}/// Turns right or left, returns the new heading/// For direction, 1 means turn right, 0 means turn left/// No need for a "clever" turning function I think.fnturn(heading:Heading,direction:isize)->Heading{ifdirection==1{matchheading{(0,1)=>(1,0),(1,0)=>(0,-1),(0,-1)=>(-1,0),(-1,0)=>(0,1),_=>panic!("Unrecognized heading!")}}else{matchheading{(0,1)=>(-1,0),(-1,0)=>(0,-1),(0,-1)=>(1,0),(1,0)=>(0,1),_=>panic!("Unrecognized heading!")}}}/// Paints a spacecraft hull using a painter bot that reads the current/// tile color to decide what to do nextfnpaint_hull(bot:&mutIntcodeInterpreter)->HashMap<Position,Color>{letmuttiles:HashMap<Position,Color>=HashMap::new();letmutposition=(0,0);tiles.insert(position,Color::White);letmutheading=(0,1);loop{// Read the tile color and pass it to the botletinput=matchtiles.entry(position).or_insert(Color::Black){Color::Black=>0,Color::White=>1,};bot.push_input(input);// Process inputbot.run();// Read the color to paint and direction to turn from the botletoutput_color=matchbot.shift_output(){1=>Color::White,0=>Color::Black,_=>panic!("Bad bot color output")};letoutput_turn=bot.shift_output();tiles.insert(position,output_color);heading=turn(heading,output_turn);position=(position.0+heading.0,position.1+heading.1);// Return if the bot has halted completelyifbot.current_instruction()==99{break;}}tiles}/// I cheated and printed the tiles map out to see the ranges of x and y/// Prints black tiles as plain terminal and white as #fndisplay_tiles(muttiles:HashMap<Position,Color>){forrowin(-5..=0).rev(){forcolin0..42{letout=matchtiles.entry((col,row)).or_insert(Color::Black){Color::White=>"#",Color::Black=>" ",};print!("{}",out);}println!("");}}pubfnrun(){letmutbot=IntcodeInterpreter::new(parse_input());lettiles=paint_hull(&mutbot);println!("Touched {} squares",tiles.len());display_tiles(tiles);}
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.
Not too bad. Follow the steps: Input -> Process -> Output -> Repeat and keep track of the touched squares in a hashmap. Could have gone full-hog and made structs out of points and headings, but this makes sorting easier. Here's my Rust solution: