Rust solution with more recursion than I do normally.
I had to create 3 variables to store the numbers because of annoying lifetime problems among other things.
usestd::fs;usestd::iter::FromIterator;fnparse(toparse:&str)->usize{toparse.parse::<usize>().unwrap()}fnpart1(data:&Vec<usize>,index:usize)->usize{letmuthaspassed:bool=false;letnum=data[index];letprevious25=Vec::from_iter(data[index-25..index].iter().cloned());foriin&previous25{forlin&previous25{ifi+l==num{ifi!=l{haspassed=true;}}}}ifhaspassed==true{part1(data,index+1)}else{num}}fnpart2(data:Vec<usize>,num:&usize,mutindex:usize,mutlen:usize)->usize{ifdata.get(index+len)==None{index=0;len+=1;}letcurrentvec:Vec<usize>=Vec::from_iter(data[index..index+len].iter().cloned());letcurrentnum:usize=currentvec.iter().sum();ifcurrentnum==*num{returnmatchcurrentvec.iter().min(){Some(x)=>x,None=>&0}+matchcurrentvec.iter().max(){Some(x)=>x,None=>&0}}else{returnpart2(data,num,index+1,len)}}fnmain(){letdata=fs::read_to_string("./day9.txt").unwrap();letmutdata2=data.split("\n").collect::<Vec<_>>();// Sometimes EOF is counted as a newline?ifmatchdata2.last(){Some(x)=>ifx==&""{true}else{false},_=>false}{data2.pop();}letparseddata=data2.iter().map(|x|parse(x)).collect::<Vec<_>>();letpart1answer=part1(&parseddata,25);letpart2answer=part2(parseddata,&part1answer,0,2);println!("Part 1: {}\nPart 2: {}",part1answer,part2answer);}
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.
Rust solution with more recursion than I do normally.
I had to create 3 variables to store the numbers because of annoying lifetime problems among other things.