settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Bomberman Win! - Java Task

+9 votes

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

Input

The input data should be read from the console. On the first and only input line you will receive the text.

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output

The destroyed text should be printed on the console.

Constraints

  • The length of the text will be in the range [1...1000].
  • The bombs will hold a number of characters in the range [0…100].
  • Bombs will not be nested (i.e. bomb inside another bomb).
  • Bomb explosions will never overlap with other bombs.
  • Time limit: 0.3 sec. Memory limit: 16 MB.

Examples

Input

Output

prepare|yo|dong

prepa........ng

Input

Ouput

de_dust2 |A| the best |BB|map!

de_d.............bes........p!

 

asked in Java category by user eiorgert
edited by user golearnweb

2 Answers

+4 votes
 
Best answer

Here's my solution:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Pr_02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String line = scanner.nextLine();//GETTING THE INPUT LINE

        Pattern pattern = Pattern.compile("\\|(.*?)\\|");//USING THE REGEX TO GET EVERYTHING (WITH .) BETWEEN THE PIPES | | USE: http://regexr.com/ to generate the rgx
        Matcher matcher = pattern.matcher(line);

        while (matcher.find()) {
            String bomb = matcher.group(1);//GETTING WHAT IS BETWEEN THE PIPES | | WITHOUT THE PIPES THEMSELVES (THAT IS WHY THE BRACKETS () in the regex WERE USED - TO FORM A GROUP)

            int bombSize = 0;
            for (int i = 0; i < bomb.length(); i++) {//CALCULATING AND ADDING EACH CHARACTER WHICH IS INSIDE THE BOMB, KNOWING THAT THE STRING IS AN ARRAY OF CHARACTERS with their numeric value
                bombSize += bomb.charAt(i);
            }

            //System.out.println(bombSize);//y (121) + o (111) = 232

            bombSize = bombSize % 10;//GETTING THE LAST DIGIT FROM THE bombSize BY MODULE DIVIDING (232 % 10 = 2)

            String regex = ".{0," + bombSize + "}\\|(.*?)\\|.{0," + bombSize + "}";//IN THE REGEX GETTING WHAT IS GOING TO BE BLOWN - HOW MANY CHARACTERS ON THE LEFT AND ON THE RIGHT - FOR THE UPPER EXAMPLE IT WILL LOOK LIKE THIS: .{0,2}\|(.*?)\|.{0,2}

            Pattern bombAreaPat = Pattern.compile(regex);
            Matcher bombAreaMatch = bombAreaPat.matcher(line);//MATCHING EVERYTHING IN THE LINE (THE INITIAL INPUT)

            boolean foundTheArea = bombAreaMatch.find();

            if (foundTheArea) {
                String area = bombAreaMatch.group();
                String dots = area.replaceAll(".", "\\.");//REPLACE ALL (.) WITH A DOT . (THAT IS WHY THE ESCAPING HERE IS NEEDED \\.)

                line = line.replace(area, dots);
            }
        }
        System.out.println(line);
    }
}

BTW - I've explained most of the code in the comments which you can read :-)

answered by user hues
edited by user golearnweb
+3 votes

Here's mine without the regex:

import java.util.Scanner;

public class Bomberman {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        StringBuilder text = new StringBuilder(input.nextLine());

        int currentIndex = 0;
        int bombStartIndex = 0;
        int bombEndIndex;

        while ((bombStartIndex = text.indexOf("|", currentIndex)) != -1) {
            bombEndIndex = text.indexOf("|", bombStartIndex + 1);
            String bombContent = text.substring(bombStartIndex + 1, bombEndIndex);

            int bombPower = getBombPower(bombContent);
            int startIndex = Math.max(0, bombStartIndex - bombPower);
            int endIndex = Math.min(text.length() - 1, bombEndIndex + bombPower);

            while (startIndex <= endIndex) {
                text.setCharAt(startIndex, '.');
                startIndex++;
            }
            currentIndex = bombEndIndex + 1;
        }
        System.out.println(text);
    }

    private static int getBombPower(String bombContent) {
        int bombPower = 0;
        for (char ch : bombContent.toCharArray()) {
            bombPower += ch;
        }
        return bombPower % 10;
    }
}

 

answered by user richard8502
...