3
error: {ballerina}IteratorMutabilityError {"message":"Table was mutated after the iterator was created"}
        at ballerina.lang.table.0:externNext(internal.bal:42)
           ballerina.lang.table.0.TableIterator:next(internal.bal:39)
           cygni.app.0:solutionPart1(app.bal:88)

in code where line 88 is foreach var p in possible

function solutionPart1() returns int|error {
  int time = 0;
  table<Position> key(x,y) possible = table [];
  possible.add('start);
  while !possible.hasKey([end.x, end.y]) {
    time = time + 1;
    table<Position> key(x,y) next = table [];
    foreach var p in possible {
      next.put({x: p.x, y: p.y});
      next.put({x: p.x + 1, y: p.y});
      next.put({x: p.x - 1, y: p.y});
      next.put({x: p.x, y: p.y + 1});
      next.put({x: p.x, y: p.y - 1});
    }
    var _ = next.removeIfHasKey(['start.x, 'start.y - 1]);
    foreach var w in winds {
      var _ = next.removeIfHasKey(check windAtTime(time, w));
    }
    foreach var r in rocks {
      var _ = next.removeIfHasKey([r.x, r.y]);
    }
    possible = next;
  }
  return time;
}

I expect this code to work, or if it is wrong, to blow up immediately. When I put in debug printouts, I see the while loop being executed several times before the error occurs.

I think this is an implementation bug in Ballerina. If not, please explain how to correct this problem.

Tobega
  • 45
  • 5

0 Answers0