Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. var Board = {
  12. init: function (n) {
  13. this.bound = n;
  14. this.ships = {};
  15. this.sunk = 0;
  16. this.hitted = 0;
  17. },
  18.  
  19. getPos: function (x, y) {
  20. x = parseInt(x, 10) - 1;
  21. y = y.charCodeAt(0) - 97;
  22. if (x >= this.bound || y >= this.bound)
  23. throw new Error('Ship part position wrong (out of bound)');
  24. return [ x, y ];
  25. },
  26.  
  27. placeShip: function (...parts) {
  28. let ship = '';
  29. let pos = [];
  30. parts.forEach((part) => {
  31. part = part.match(/^(\d+)([a-z])$/i);
  32. if (part.length !== 3 || part[2].charCodeAt(0) < 97 || part[2].charCodeAt(0) > 122)
  33. throw new Error('Ship part position wrong');
  34. let [ x, y ] = this.getPos(part[1], part[2]);
  35. ship += `${x}${y}`;
  36. if(pos.indexOf(`${x}${y}`) === -1)
  37. pos.push(`${x}${y}`);
  38. });
  39. this.ships[ship] = pos;
  40. },
  41.  
  42. placeShips: function (ships) {
  43. ships.forEach((ship) => {
  44. this.placeShip(...(ship.split(' ')));
  45. });
  46. },
  47.  
  48. destroyAt: function (x, y) {
  49. for(ship in this.ships) {
  50. const indx = this.ships[ship].indexOf(`${x}${y}`);
  51. if(indx >= 0) {
  52. this.ships[ship].splice(indx, 1);
  53. if (this.ships[ship].length === 0) {
  54. ++this.sunk;
  55. } else if(!this.ships[ship].hitted) {
  56. ++this.hitted;
  57. this.ships[ship].hitted = true;
  58. }
  59. }
  60. }
  61. },
  62.  
  63. attack: function (positions) {
  64. positions.forEach((position) => {
  65. position = position.match(/^(\d+)([a-z])$/i);
  66. if (position.length !== 3 || position[2].charCodeAt(0) < 97 || position[2].charCodeAt(0) > 122)
  67. throw new Error('Attack part position wrong');
  68. let [ x, y ] = this.getPos(position[1], position[2]);
  69. this.destroyAt(x, y);
  70. });
  71. }
  72. };
  73.  
  74. function solution (N, S, T) {
  75. Board.init(N);
  76. Board.placeShips(S.toLowerCase().split(','));
  77. Board.attack(T.toLowerCase().split(' '));
  78. return `${Board.sunk},${Board.hitted}`;
  79. }
  80.  
  81. console.log(solution(4, '1B 1c,2d 4d', '2b 2d 3d 4d 4a'));
  82.  
  83. console.log(solution(3, '1A 1B,2c 2c', '1b'));
  84.  
  85. console.log(solution(12, '1A 2A,12A 12A', '12A'));
  86. </script>
  87.  
  88.  
  89.  
  90. <script id="jsbin-source-javascript" type="text/javascript">var Board = {
  91. init: function (n) {
  92. this.bound = n;
  93. this.ships = {};
  94. this.sunk = 0;
  95. this.hitted = 0;
  96. },
  97.  
  98. getPos: function (x, y) {
  99. x = parseInt(x, 10) - 1;
  100. y = y.charCodeAt(0) - 97;
  101. if (x >= this.bound || y >= this.bound)
  102. throw new Error('Ship part position wrong (out of bound)');
  103. return [ x, y ];
  104. },
  105.  
  106. placeShip: function (...parts) {
  107. let ship = '';
  108. let pos = [];
  109. parts.forEach((part) => {
  110. part = part.match(/^(\d+)([a-z])$/i);
  111. if (part.length !== 3 || part[2].charCodeAt(0) < 97 || part[2].charCodeAt(0) > 122)
  112. throw new Error('Ship part position wrong');
  113. let [ x, y ] = this.getPos(part[1], part[2]);
  114. ship += `${x}${y}`;
  115. if(pos.indexOf(`${x}${y}`) === -1)
  116. pos.push(`${x}${y}`);
  117. });
  118. this.ships[ship] = pos;
  119. },
  120.  
  121. placeShips: function (ships) {
  122. ships.forEach((ship) => {
  123. this.placeShip(...(ship.split(' ')));
  124. });
  125. },
  126.  
  127. destroyAt: function (x, y) {
  128. for(ship in this.ships) {
  129. const indx = this.ships[ship].indexOf(`${x}${y}`);
  130. if(indx >= 0) {
  131. this.ships[ship].splice(indx, 1);
  132. if (this.ships[ship].length === 0) {
  133. ++this.sunk;
  134. } else if(!this.ships[ship].hitted) {
  135. ++this.hitted;
  136. this.ships[ship].hitted = true;
  137. }
  138. }
  139. }
  140. },
  141.  
  142. attack: function (positions) {
  143. positions.forEach((position) => {
  144. position = position.match(/^(\d+)([a-z])$/i);
  145. if (position.length !== 3 || position[2].charCodeAt(0) < 97 || position[2].charCodeAt(0) > 122)
  146. throw new Error('Attack part position wrong');
  147. let [ x, y ] = this.getPos(position[1], position[2]);
  148. this.destroyAt(x, y);
  149. });
  150. }
  151. };
  152.  
  153. function solution (N, S, T) {
  154. Board.init(N);
  155. Board.placeShips(S.toLowerCase().split(','));
  156. Board.attack(T.toLowerCase().split(' '));
  157. return `${Board.sunk},${Board.hitted}`;
  158. }
  159.  
  160. console.log(solution(4, '1B 1c,2d 4d', '2b 2d 3d 4d 4a'));
  161.  
  162. console.log(solution(3, '1A 1B,2c 2c', '1b'));
  163.  
  164. console.log(solution(12, '1A 2A,12A 12A', '12A'));
  165. </script></body>
  166. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement