Sort by

recency

|

364 Discussions

|

  • + 0 comments

    My solution:

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'bomberMan' function below.
    #
    # The function is expected to return a STRING_ARRAY.
    # The function accepts following parameters:
    #  1. INTEGER n
    #  2. STRING_ARRAY grid
    #
        
          
    def _to_strings(grid):
        res=[]
        for s in grid:
            res.append(''.join(['.' if x<=0 else 'O' for x in s]))
        return res
        
        
    def _detonate(grid, i, j):
        grid[i][j]=0
        if i>0 and grid[i-1][j]!=1:
            grid[i-1][j]=0
        if j>0 and grid[i][j-1]!=1:
            grid[i][j-1]=0
        if i<len(grid)-1 and grid[i+1][j]!=1:
            grid[i+1][j]=0
        if j<len(grid[0])-1 and grid[i][j+1]!=1:
            grid[i][j+1]=0
        
        
    def _plant(grid):
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if (grid[i][j]<=0):
                    grid[i][j]=3
        pass
        
            
    def _next_tick(grid, tick):
        if tick==2:
            return
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j]>1:
                    grid[i][j]-=1
        if tick==1 or tick%2 == 0 and tick!=2 :
            _plant(grid)
            return
      
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j]==1:
                    _detonate(grid, i, j)
                    
                   
    def _to_grid(grid):
        res=[]
        for s in grid:
            res.append([0 if x=='.' else 3 for x in s])
        return res
    
    def _to_grid(grid):
        res=[]
        for s in grid:
            res.append([0 if x=='.' else 3 for x in s])
        return res 
    
        
    def bomberMan(n, grid):
        if n%2==0:
            return ['O'*len(grid[0])]*len(grid)
        grid=_to_grid(grid)
        if n==1:
            return _to_strings(grid)
        for i in range(1, min(8, 4+n%4+1)):
            _next_tick(grid, i)
        return _to_strings(grid)
    
    
        
        # Write your code here
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
    
        r = int(first_multiple_input[0])
    
        c = int(first_multiple_input[1])
    
        n = int(first_multiple_input[2])
    
        grid = []
    
        for _ in range(r):
            grid_item = input()
            grid.append(grid_item)
    
        result = bomberMan(n, grid)
    
        fptr.write('\n'.join(result))
        fptr.write('\n')
    
        fptr.close()
    
    
    
        fptr.close()
    
  • + 0 comments

    def bomberMan(n, grid): if n% 2 == 0: return ["O"*len(grid[0])]*len(grid)

    k = 1 if (n-1)% 4 != 0 else (0 if n==1 else 2)
    for _ in range(k):
        d1 = []
        d2 = []
        grid_2 = [''.join(x) for x in zip(*grid)]
        for nap, dn in ((grid, d1), (grid_2, d2)):
            for i in nap:
                gr = i.replace(".", '0').replace("O", '1')
                gr2 = int(gr, 2) | int(gr, 2) <<1
                gr3 = int(gr +'0', 2) | gr2 >>1
                dn.append(bin(gr3)[2:].zfill(len(i) + 1)[1:])
        d3 = [''.join(x) for x in zip(*d2)]
        d4 = [(int(p, 2) | int(v, 2)) for (p, v) in zip(d1, d3)]
        d4s = [bin(x + 2 ** len(grid[0]))[3:] for x in d4]
        grid = [i.replace("0", "O").replace("1", ".") for i in d4s]
    
    return grid
    
  • + 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