Sort by

recency

|

362 Discussions

|

  • + 0 comments

    swift:

    func bomberMan(n: Int, grid: [String]) -> [String] {
        // Write your code here    
        var currentGrid: [[UInt8]] = grid.map{ [UInt8]($0.utf8) }
        var results: [[[UInt8]]] = [currentGrid]
        
        let resultIndex: Int = abs((n % 4) - 1)
        let allBombs: [[UInt8]] = [[UInt8]](repeating: [UInt8](repeating: 79, count: currentGrid.first!.count), count: currentGrid.count) 
        
        if n == 1 {
            return grid
        }
        
        if resultIndex % 2 == 1 {
            return allBombs.map{ String(bytes: $0, encoding: String.Encoding.utf8)! }
        }
        
        while(true) {
            var result: [[UInt8]] = allBombs 
            for index1 in 0 ... currentGrid.count - 1 {
                for index2 in 0 ... currentGrid[index1].count - 1 {
                    if currentGrid[index1][index2] == 79 {
                        if index2 - 1 >= 0 {
                            result[index1][index2 - 1] = 46
                        }
                        if index2 + 1 <= currentGrid[index1].count - 1 {
                            result[index1][index2 + 1] = 46
                        }
    
                        if index1 > 0 {
                            result[index1 - 1][index2] = 46
                        }
                        if index1 + 1 <= currentGrid.count - 1 {
                            result[index1 + 1][index2] = 46
                        }       
                        result[index1][index2] = 46
                    }
                }
            }
            
            if results.firstIndex(where: { $0 == result }) != nil {
                break
            }
            results.append(result)
            currentGrid = result
        }
        
        if results.count > 2 {
            results.removeFirst()
            results.swapAt(0, 1)
        }
                
        return results[resultIndex / 2].map{ String(bytes: $0, encoding: String.Encoding.utf8)! }
    }
    
  • + 1 comment
    function bomberMan(n, gridInit) {
        
        // Grid will have 3 states which are going to get repeated over and over
        // 1. Grid before explosion ::: gridBeforeExplosion
        // 2. Grid full of bombs ::: gridFullOfBombs
        // 3. Grid after explosion ::: gridAfterExplosion
            
        let gridBeforeExplosion = [...gridInit];
        if(n === 1) return gridBeforeExplosion; // Nothing happens at second 1
        
        const gridFullOfBombs = plantBombs(gridBeforeExplosion); 
        if(n % 2 === 0) return gridFullOfBombs.map( v => v.join('')); // Every pair second bomberman fills the grid with bombs
        
        // from this point onwards every unpair number of seconds we alternate between 2 states
        // n = 3, 7, 11, 15, 19, 23, ... will always be the same ( (n - 3) % 4 === 0 )
        // n = 5, 9, 13, 17, 21, 25, ... will always be the same ( (n - 5) % 4 === 0 )
        let gridAfterExplosion = explosion(gridBeforeExplosion, gridFullOfBombs);
        if((n - 3) % 4 === 0) return gridAfterExplosion;
        
        gridBeforeExplosion = [...gridAfterExplosion];
        gridAfterExplosion = explosion(gridBeforeExplosion, gridFullOfBombs);
        
        return gridAfterExplosion;
    }
    
  • + 0 comments

    Python 3

    def bomberMan(n, grid):
        if n == 1:
            #No changes
            return grid
        if n%2 == 0:
            #All spaces have bombs
            return ["O"*c for _ in range(r)]
        pattern = re.compile(r'(?=O)')
        #There are two patterns for each odd number after 1
        #Pattern is determined by "(n//2) % 2"
        for _ in range(2 - (n//2)%2):
            detonations = set()
            #Get the locations of the detonations
            for match in pattern.finditer("\n".join(grid)):
                y = match.start()//(c+1)
                x = match.start()%(c+1)
                detonations.add((y, x))
                if y != 0:
                    detonations.add((y-1, x))
                if y != r-1:
                    detonations.add((y+1, x))
                if x != 0:
                    detonations.add((y, x-1))
                if x != c-1:
                    detonations.add((y, x+1))
            grid = ["" for _ in range(r)]
            for y in range(r):
                for x in range(c):
                    if (y,x) in detonations:
                        grid[y] += "."
                    else:
                        grid[y] += "O"
        return grid
    
  • + 0 comments

    C++

    void explode(vector<string>* grid, vector<string>* result){
        for(int row = 0; row<(*grid).size(); row++){
            for(int col = 0; col<(*grid)[0].size(); col++){
                if((*grid)[row][col] == 'O') {
                    (*result)[row][col] = '.';
                    if (col-1>=0) (*result)[row][col-1] = '.';
                    if (row-1>=0) (*result)[row-1][col] = '.';
                    if (col+1<(*result)[0].size()) (*result)[row][col+1] = '.';
                    if (row+1<(*result).size()) (*result)[row+1][col] = '.';
                }
            }
         }
    }
    
    vector<string> bomberMan(int n, vector<string> grid) {
        //These solutions are trivial
        if(n==1) return grid;
        string row(grid[0].size(), 'O');
        vector<string> result(grid.size(), row);
        if(n%2==0)return result;
        /*The first detonation and second detonation patterns keep repeating.
        The only thing I need to check is if n belongs to the arithmetic 
        sequence with d=4 and a1=3 (first detonation) or a1=5 (second detonation).*/
        explode(&grid, &result);
        if((n-3)%4==0) return result;
        grid = result;
        fill(result.begin(), result.end(), row);
        if((n-5)%4==0){
            explode(&grid, &result);
            return result;
        }
        return {"*"};
    }
    
  • + 0 comments
    std::vector<std::string> bomberMan(int n, std::vector<std::string> grid)
    {
      auto blowTargets = [](std::vector<std::string> const& targets)
        {
          auto rows = targets.size();
          auto columns = targets.front().size();
          // This mixes the step where we plant bombs in the whole grid 
          // and then we blow up the previous bombs...
          std::vector<std::string> ret(targets.size(), std::string(columns, 'O'));
    
          for (int r = 0; r < rows; ++r)
          {
            for (int j = 0; j < columns; ++j)
            {
              if (targets[r][j] != 'O')
                continue;
              ret[r][j] = '.';
              if (j > 0)
                ret[r][j - 1] = '.'; // LEFT
              if (j + 1 < columns)
                ret[r][j + 1] = '.'; // RIGHT
              if (r > 0)
                ret[r - 1][j] = '.'; // UP
              if (r + 1 < rows)
                ret[r + 1][j] = '.'; // DOWN
            }
          }
          return ret;
        };
    
      if (n < 3)
        return grid;
    
      if (n % 2 == 0) // On even seconds, the entire grid is filled with bombs.
      {
        return std::vector<std::string>(grid.size(), std::string(grid.front().size(), 'O'));
      }
    
      if (n % 4 == 3)
        return blowTargets(grid);
    
      return blowTargets(blowTargets(grid));
    }