Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- var Board = {
- init: function (n) {
- this.bound = n;
- this.ships = {};
- this.sunk = 0;
- this.hitted = 0;
- },
- getPos: function (x, y) {
- x = parseInt(x, 10) - 1;
- y = y.charCodeAt(0) - 97;
- if (x >= this.bound || y >= this.bound)
- throw new Error('Ship part position wrong (out of bound)');
- return [ x, y ];
- },
- placeShip: function (...parts) {
- let ship = '';
- let pos = [];
- parts.forEach((part) => {
- part = part.match(/^(\d+)([a-z])$/i);
- if (part.length !== 3 || part[2].charCodeAt(0) < 97 || part[2].charCodeAt(0) > 122)
- throw new Error('Ship part position wrong');
- let [ x, y ] = this.getPos(part[1], part[2]);
- ship += `${x}${y}`;
- if(pos.indexOf(`${x}${y}`) === -1)
- pos.push(`${x}${y}`);
- });
- this.ships[ship] = pos;
- },
- placeShips: function (ships) {
- ships.forEach((ship) => {
- this.placeShip(...(ship.split(' ')));
- });
- },
- destroyAt: function (x, y) {
- for(ship in this.ships) {
- const indx = this.ships[ship].indexOf(`${x}${y}`);
- if(indx >= 0) {
- this.ships[ship].splice(indx, 1);
- if (this.ships[ship].length === 0) {
- ++this.sunk;
- } else if(!this.ships[ship].hitted) {
- ++this.hitted;
- this.ships[ship].hitted = true;
- }
- }
- }
- },
- attack: function (positions) {
- positions.forEach((position) => {
- position = position.match(/^(\d+)([a-z])$/i);
- if (position.length !== 3 || position[2].charCodeAt(0) < 97 || position[2].charCodeAt(0) > 122)
- throw new Error('Attack part position wrong');
- let [ x, y ] = this.getPos(position[1], position[2]);
- this.destroyAt(x, y);
- });
- }
- };
- function solution (N, S, T) {
- Board.init(N);
- Board.placeShips(S.toLowerCase().split(','));
- Board.attack(T.toLowerCase().split(' '));
- return `${Board.sunk},${Board.hitted}`;
- }
- console.log(solution(4, '1B 1c,2d 4d', '2b 2d 3d 4d 4a'));
- console.log(solution(3, '1A 1B,2c 2c', '1b'));
- console.log(solution(12, '1A 2A,12A 12A', '12A'));
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">var Board = {
- init: function (n) {
- this.bound = n;
- this.ships = {};
- this.sunk = 0;
- this.hitted = 0;
- },
- getPos: function (x, y) {
- x = parseInt(x, 10) - 1;
- y = y.charCodeAt(0) - 97;
- if (x >= this.bound || y >= this.bound)
- throw new Error('Ship part position wrong (out of bound)');
- return [ x, y ];
- },
- placeShip: function (...parts) {
- let ship = '';
- let pos = [];
- parts.forEach((part) => {
- part = part.match(/^(\d+)([a-z])$/i);
- if (part.length !== 3 || part[2].charCodeAt(0) < 97 || part[2].charCodeAt(0) > 122)
- throw new Error('Ship part position wrong');
- let [ x, y ] = this.getPos(part[1], part[2]);
- ship += `${x}${y}`;
- if(pos.indexOf(`${x}${y}`) === -1)
- pos.push(`${x}${y}`);
- });
- this.ships[ship] = pos;
- },
- placeShips: function (ships) {
- ships.forEach((ship) => {
- this.placeShip(...(ship.split(' ')));
- });
- },
- destroyAt: function (x, y) {
- for(ship in this.ships) {
- const indx = this.ships[ship].indexOf(`${x}${y}`);
- if(indx >= 0) {
- this.ships[ship].splice(indx, 1);
- if (this.ships[ship].length === 0) {
- ++this.sunk;
- } else if(!this.ships[ship].hitted) {
- ++this.hitted;
- this.ships[ship].hitted = true;
- }
- }
- }
- },
- attack: function (positions) {
- positions.forEach((position) => {
- position = position.match(/^(\d+)([a-z])$/i);
- if (position.length !== 3 || position[2].charCodeAt(0) < 97 || position[2].charCodeAt(0) > 122)
- throw new Error('Attack part position wrong');
- let [ x, y ] = this.getPos(position[1], position[2]);
- this.destroyAt(x, y);
- });
- }
- };
- function solution (N, S, T) {
- Board.init(N);
- Board.placeShips(S.toLowerCase().split(','));
- Board.attack(T.toLowerCase().split(' '));
- return `${Board.sunk},${Board.hitted}`;
- }
- console.log(solution(4, '1B 1c,2d 4d', '2b 2d 3d 4d 4a'));
- console.log(solution(3, '1A 1B,2c 2c', '1b'));
- console.log(solution(12, '1A 2A,12A 12A', '12A'));
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement