Part two of this really needed some performance work to finish. The next_recipes method does the work of moving the elves from their current scores to the next. Then the loops are built differently to return the final result.
This is my attempt in Crystal:
classRecipedefself.next_ten(after:String)after=after.to_irecipe_list=[3,7]elf1=0elf2=1whilerecipe_list.size<after+10recipe_list,elf1,elf2=next_recipes(recipe_list,elf1,elf2)endreturnrecipe_list[after...after+10].join("")enddefself.how_many_to_score(score:String)pattern=score.split("").map(&.to_i)size=pattern.sizerecipe_list=[3,7]elf1=0elf2=1whilerecipe_list.size<size+1recipe_list,elf1,elf2=next_recipes(recipe_list,elf1,elf2)endwhilerecipe_list[-size..-1]!=pattern&&recipe_list[-(size+1)..-2]!=patternrecipe_list,elf1,elf2=next_recipes(recipe_list,elf1,elf2)endrecipe_string=recipe_list.join("")returnrecipe_string.index(score)enddefself.next_recipes(list,elf1,elf2)combine=list[elf1]+list[elf2]ifcombine<10list.push(combine)elselist.push(combine/10%10).push(combine%10)endelf1=(elf1+list[elf1]+1)%list.sizeelf2=(elf2+list[elf2]+1)%list.size{list,elf1,elf2}endendputs"--- Day 14: Chocolate Charts ---"input=File.read("./14/input.txt")puts"The next 10 scores after #{input} are: #{Recipe.next_ten(input)}"puts"The position for the pattern #{input} is #{Recipe.how_many_to_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.
Part two of this really needed some performance work to finish. The
next_recipes
method does the work of moving the elves from their current scores to the next. Then the loops are built differently to return the final result.This is my attempt in Crystal: