The part 1 was super easy: I just pushed new elements to an array.
#!/usr/bin/perlusewarnings;usestrict;usefeatureqw{ say };chomp(my$input=<>);my@scores=(3,7);my@elves=(0,1);while(@scores<11+$input){my$new=$scores[$elves[0]]+$scores[$elves[1]];push@scores,split//,$new;$elves[$_]+=1+$scores[$elves[$_]],$elves[$_]%=@scoresfor0,1;}say@scores[$input..9+$input];
When I adapted the algorithm for the part 2, it took almost a minute to get the answer. To optimize it, I used strings instead of arrays of numbers (strings are not arrays in Perl).
#!/usr/bin/perlusewarnings;usestrict;usefeatureqw{ say };chomp(my$input=<>);my$score='37';my@elves=(0,1);while(length$score<length$input||-1==rindexsubstr($score,-12),$input){my$new=substr($score,$elves[0],1)+substr$score,$elves[1],1;$score.=$new;$elves[$_]+=1+substr($score,$elves[$_],1),$elves[$_]%=length$scorefor0,1;}sayrindex$score,$input;
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.
The part 1 was super easy: I just pushed new elements to an array.
When I adapted the algorithm for the part 2, it took almost a minute to get the answer. To optimize it, I used strings instead of arrays of numbers (strings are not arrays in Perl).