Advertisement
dimipan80

Exams - Terrorists Win! (on JavaScript)

Jan 8th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* On de_dust2 terrorists have planted a bomb (or possibly several of them)! Write a program that sets
  2.  those bombs off!
  3.  A bomb is a string in the format |...|. When set off, the bomb destroys all characters inside.
  4.  The bomb should also destroy n characters to the left and right of the bomb.
  5.  n is determined by the bomb power (the last digit of the ASCII sum of the characters inside the bomb).
  6.  Destroyed characters should be replaced by '.' (dot). For example, we are given the following text:
  7.  prepare|yo|dong
  8.  The bomb is |yo|. We get the bomb power by calculating the last digit of the sum: y (121) + o (111) = 232.
  9.  The bomb explodes and destroys itself and 2 characters to the left and 2 characters to the right.
  10.  The result is:
  11.  prepa........ng
  12.  Bombs will not be nested (i.e. bomb inside another bomb).
  13.  Bomb explosions will never overlap with other bombs.
  14.  On the first and only input line you will receive the text.
  15.  The destroyed text should be printed on the console. */
  16.  
  17. "use strict";
  18.  
  19. function solve(args) {
  20.     var text = args[0].split('');
  21.     while (text.indexOf('|') > -1) {
  22.         var startBomb = text.indexOf('|');
  23.         text[startBomb] = '.';
  24.         var endBomb = text.indexOf('|');
  25.         text[endBomb] = '.';
  26.  
  27.         var sum = 0;
  28.         for (var i = startBomb + 1; i < endBomb; i += 1) {
  29.             sum += text[i].charCodeAt(0);
  30.         }
  31.  
  32.         var numStr = sum.toString(10);
  33.         var power = parseInt(numStr[numStr.length - 1]);
  34.         if (power) {
  35.             startBomb = Math.max(0, startBomb - power);
  36.             endBomb = Math.min(text.length - 1, endBomb + power);
  37.         }
  38.  
  39.         for (i = startBomb; i <= endBomb; i += 1) {
  40.             text[i] = '.';
  41.         }
  42.     }
  43.  
  44.     console.log(text.join(''));
  45. }
  46.  
  47. solve(['prepare|yo|dong']);
  48. solve(['de_dust2 |A| the best |BB|map!']);
  49. solve(['^O^ ^O^ ^o^ |ded| <--- will those kids get killed? y/n']);
  50. solve(['||well||']);
  51. solve(['>*o*> |ATOMIC BOMB| // Goodness grace, will I survive?']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement