Ok so I'll be the first to emit that while my part one was fine in Haskell it was never going to scale to part 2! I really hate mutable arrays in Haskell, I mean what's the point, and as such I wrote the 2nd part in rust.
constN:usize=1000000;constM:usize=10000000;fnin_holding(x:usize,holding:&[usize;3])->bool{foriin0..3{ifx==holding[i]{returntrue;}}false}fntask2()->usize{letmutv:Vec<usize>=Vec::with_capacity(N);foriin0..N{v.push(i+1);}// build the beginning of our "linked" list with my input: 219748365v[0]=2;v[2]=1;v[1]=9;v[9]=7;v[7]=4;v[4]=8;v[8]=3;v[3]=6;v[6]=5;v[5]=10;v[N-1]=0;letmutcurr=v[0];letmutholding:[usize;3]=[0;3];for_in0..M{// "copy" 3 holdingholding[0]=v[curr];holding[1]=v[holding[0]];holding[2]=v[holding[1]];// find destinationletmutdest=ifcurr==0{N-1}else{curr-1};whilein_holding(dest,&holding){dest=dest-1;}// updatelettmp=v[dest];v[dest]=holding[0];v[curr]=v[holding[2]];v[holding[2]]=tmp;curr=v[curr];}v[1]*v[v[1]]}fnmain(){println!("{}",task2());}
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.
Ok so I'll be the first to emit that while my part one was fine in Haskell it was never going to scale to part 2! I really hate mutable arrays in Haskell, I mean what's the point, and as such I wrote the 2nd part in rust.
Here's Haskell for part 1:
and here's Rust for part 2: