You are reading a single comment by @wence and its replies. Click here to read the full conversation.
  • Not the most elegant, but we got there: https://gist.github.com/dmckennell/45bc2e70e2990e43e262ffbcee95f67f

    Couldn't avoid running the program a second time in part 2 when I discover the program is completing successfully following a swap. Would like to have memoized the program result used in my if condition, but not seeing an easy way to manage that with collectFirst.

  • Not really au-fait with Scala but I would have thought you could pattern-match on the result of swapAndExecute? Almost certainly not correct syntax, but something like

        input.zipWithIndex.collectFirst {
          case (instruction, index) 
             swapAndExecute(input, instruction, index) match {
               case LoopResult(acc, Completed) => acc,
               case other => None,
          }
    }.get
    
  • Sadly no - collectFirst takes a partial function (in this case PartialFunction[(Instruction, Int), Int]) which I can't wrangle the input collection into. It's a bit of a 'square peg round hole' situation. I should probably craft something myself for such a case.

    In your code snippet the case would be satisfied on the first instruction and I would then get a NoSuchElementException.

About

Avatar for wence @wence started