Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* On de_dust2 terrorists have planted a bomb (or possibly several of them)! Write a program that sets
- those bombs off!
- A bomb is a string in the format |...|. When set off, the bomb destroys all characters inside.
- The bomb should also destroy n characters to the left and right of the bomb.
- n is determined by the bomb power (the last digit of the ASCII sum of the characters inside the bomb).
- Destroyed characters should be replaced by '.' (dot). For example, we are given the following text:
- prepare|yo|dong
- The bomb is |yo|. We get the bomb power by calculating the last digit of the sum: y (121) + o (111) = 232.
- The bomb explodes and destroys itself and 2 characters to the left and 2 characters to the right.
- The result is:
- prepa........ng
- Bombs will not be nested (i.e. bomb inside another bomb).
- Bomb explosions will never overlap with other bombs.
- On the first and only input line you will receive the text.
- The destroyed text should be printed on the console. */
- "use strict";
- function solve(args) {
- var text = args[0].split('');
- while (text.indexOf('|') > -1) {
- var startBomb = text.indexOf('|');
- text[startBomb] = '.';
- var endBomb = text.indexOf('|');
- text[endBomb] = '.';
- var sum = 0;
- for (var i = startBomb + 1; i < endBomb; i += 1) {
- sum += text[i].charCodeAt(0);
- }
- var numStr = sum.toString(10);
- var power = parseInt(numStr[numStr.length - 1]);
- if (power) {
- startBomb = Math.max(0, startBomb - power);
- endBomb = Math.min(text.length - 1, endBomb + power);
- }
- for (i = startBomb; i <= endBomb; i += 1) {
- text[i] = '.';
- }
- }
- console.log(text.join(''));
- }
- solve(['prepare|yo|dong']);
- solve(['de_dust2 |A| the best |BB|map!']);
- solve(['^O^ ^O^ ^o^ |ded| <--- will those kids get killed? y/n']);
- solve(['||well||']);
- solve(['>*o*> |ATOMIC BOMB| // Goodness grace, will I survive?']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement