Esolang Interpreter. Help needed! (Solved)

Esolang Interpreter. Help needed! (Solved)
0.0 0

#1

I am currently stuck on a kata on codewars. The full description of the task can be found here: https://www.codewars.com/kata/esolang-interpreters-number-3-custom-paintf-star-star-k-interpreter.

All the tests pass exept one and I have no idea why. So I figured a couple more eyes are probably helpful in such a case. This is the case that doesnt pass:

  • input: interpreter("*[s[e]*]", 49, 5, 5);
  • expected: "11100\r\n11100\r\n11000\r\n11000\r\n11000"
  • actual: "11100\r\n11100\r\n11100\r\n11000\r\n11000"

If we visualize the grid it basically lookes like this:

expected

11100
11100
11000
11000
11000

actual

11100
11100
11100
11000
11000

I have made a gist of my current solution on github. You can download the file and test the code with node paintfoo.js "*[s[e]*]" 49 5 5 (the command line parameters get passed to the interpreter() function) Any help is much appreciated.

EDIT:

I have found the bug! I misunderstood the instruction on what to count as an iteration and what not. In the description it says

Regarding iterations, the act of skipping to the matching ] when a [ is encountered (or vice versa) is considered to be one iteration regardless of the number of command characters in between.

And here are the instructions on skipping

  • [ Jump past matching ] if bit under current pointer is 0
  • ] Jump back to the matching [ (if bit under current pointer is nonzero)

I thought that an iteration only counts if we actually jump (i.e if bit is zero and nonzero for jumping back) It turns out that an iteration should count even if we not actually jump. That was my first mistake.

And here is what made the bug so hard to detect. If you read the instructions for [ and ] encounters notice that at the ] it says jump back to the matching [. It turns out we should jump back past the matching [ and not to. (PAST vs TO) This scenario often lead to my skipped iteration that I didn’t count being cancled out (I don’t count the iteration but then my program has to make one more instruction because I jumped back to [ and not past it so this counts as an additional iteration and they cancel out)