Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. function hackTAS(S) {
  2. console.time()
  3. // get the row length
  4. w = S[0].length + 1
  5. // join board rows to 1d array with a wall ('#') to deny horizonthal moves between rows
  6. S = S.join`#`
  7. // add an id to each item indexed by the it's coordinates
  8. // if there is more than 9 items, then extend the id with some separator for example: ':'
  9. T = {}
  10. id = 1
  11. for (i = 0; i < S.length; i++)
  12. if (!"# SE".includes(S[i])) {
  13. T[i] = id + ':'
  14. id++
  15. }
  16. // initial board. key is "0"
  17. B = [Buffer(S)]
  18. // initial queue: [position, board key (or mask), hitpoint, level (or move count), fish]
  19. Q = [[S.indexOf('S'), "0", 1, 0, 0]]
  20. // directions for simple moves and with fish moves
  21. D = [[[-w],[-1],[1],[w]],
  22. [[-w,-2*w],[-1,-2,-3],[1,2,3],[w,2*w]]
  23. ]
  24. // process the queue
  25. for ([p, m, h, s, f] of Q) {
  26.  
  27. // debug
  28. //console.log("p", p, "m", m, "s", s, "h", h, f ? "fish": "");
  29. //(B[m] + "").match(RegExp(".{"+w+"}", "g")).map(s => console.log(s))
  30.  
  31. // loop on the four direction ULRD
  32. for (i of D[f]) {
  33. // keep the original variables
  34. mm = m
  35. hh = h
  36. ff = f
  37. // loop on moves in distance (the reason, fish can move 1,2,3 cells horizontally and 1,2 in vertically)
  38. for (j of i) {
  39. // get cell value
  40. v = B[m][j += p]
  41. // check the exit point
  42. if (v == 69){
  43. console.timeEnd()
  44. return s + 1
  45. }
  46. // check special items 'B', 'C', 'D', 'F'
  47. if (v > 43) {
  48. // unknow item then stop (for example 'S', we came back to the start point)
  49. if (!T[j]) break;
  50. // item 'D'
  51. if (v == 68) {
  52. // if hitpoint == 1 then increase hp
  53. hh += hh < 2
  54. // item 'F'
  55. } else if (v == 70) {
  56. // set fish flag
  57. ff = 1
  58. // item 'B'
  59. } else if (v == 66) {
  60. // decrease hp if no fish
  61. hh -= f == 0
  62. // item 'C'
  63. } else if (v == 67) {
  64. // unset fish if we have or decrease hitpoint
  65. ff ? ff = 0
  66. : hh--
  67. }
  68. // stop this thread, if we died, hp == 0
  69. if (hh == 0)
  70. break
  71. // add the new item id to the board key
  72. mmm = mm + T[j]
  73. // clone the current board to the new (if we don't have board with new key)
  74. // and clear visited cells (we can move freely to anywhere again)
  75. mmm in B
  76. ? 0
  77. : B[mmm] = Buffer((B[mm] + "").replace(/\+/g, ' '))
  78. mm = mmm
  79. // set cell to visited ('+')
  80. B[mm][j] = 43
  81. // add position to the queue
  82. Q.push([j, mm, hh, s + 1, ff])
  83. if (ff == 0) break
  84. // empty cell, ' '. we can move freely
  85. } else if (v == 32) {
  86. // set cell to visited ('+')
  87. B[mm][j] = 43
  88. // add position to the queue
  89. Q.push([j, mm, hh, s + 1, ff])
  90. // wall, '#'
  91. } else if (v < 43)
  92. // stop moving in this direction
  93. break
  94. }
  95. }
  96. }
  97. console.timeEnd()
  98. return -1
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement