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!
Woohoo! Had some time to knock together a solution during lunch! It also contains what I feel like is my most Rust-y code I've ever written, where I fold, zip, map, and into_iter all in a few lines. I'm still upset at the obscene hoops I had to go through to read a file and parse each character to a digit. But baby steps...
/// Day 8: Space Image Format/// /// Parse a layered image format being STREAMED THROUGH SPACEusestd::fs;usestd::convert::TryInto;/// Expects a single line of 0's 1's and 2'sfnparse_input()->Vec<Vec<usize>>{lettext=fs::read_to_string("data/day8.txt").unwrap();letmutresults:Vec<Vec<usize>>=vec![];letmutcurrent:Vec<usize>=vec![];letmutcounter=0;forcintext.chars(){current.push(c.to_digit(10).unwrap().try_into().unwrap());counter+=1;ifcounter==25*6{counter=0;results.push(current);current=vec![];}}results}/// To verify the parsing, find the layer with the fewest zeros and return the/// number of 1's in that layer times the number of 2's.fnpart1(layers:&Vec<Vec<usize>>)->usize{lettarget_layer:&Vec<usize>=layers.iter().min_by_key(|layer|{layer.iter().filter(|x|**x==0).count()}).unwrap();letones=target_layer.iter().filter(|x|**x==1).count();lettwos=target_layer.iter().filter(|x|**x==2).count();ones*twos}/// Stack the layers top to bottom. If a layer contains a 2, it's transparent/// and layers below can be seen. 1's are black. 0's are white./// /// Show the final compressed image.fnpart2(layers:Vec<Vec<usize>>){letstart:Vec<usize>=vec![2;25*6];letresult=layers.into_iter().fold(start,|acc,layer|{acc.into_iter().zip(layer.into_iter()).map(|(current,new)|{ifcurrent==2{new}else{current}}).collect()});forrowin0..6{forcolin0..25{print!("{}",ifresult[row*25+col]==1{"#"}else{" "});}print!("\n");}}pubfnrun(){letlayers=parse_input();println!("Part 1 checksum: {}",part1(&layers));part2(layers);}
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.
Woohoo! Had some time to knock together a solution during lunch! It also contains what I feel like is my most Rust-y code I've ever written, where I
fold
,zip
,map
, andinto_iter
all in a few lines. I'm still upset at the obscene hoops I had to go through to read a file and parse each character to a digit. But baby steps...