Sort by

recency

|

366 Discussions

|

  • + 0 comments

    N(nothing) only happens at 2 seconds. After that bombs are detonating or you are planting bombs.

    P0->N->P1->D0->P2->D1->P3->D2->P4->D3->....etc

    The question tells you this indirectly as the "nothing" is step 2, and we are told to repeat step 3 and 4. Insulation types for hot climates

  • + 0 comments

    Instructions: Here are some helpful tips:

    Convert the list into a 2D matrix of integers. Assign state codes to each cell. Use pen and paper: Start by defining an initial state for the matrix (e.g., a 3x3 grid for simplicity). Write down the matrix for each second, step by step, until you observe a repeating pattern or a cycle.

    Optimization Tips:

    Handle edge cases efficiently:

    Quickly return results for trivial cases or repeating sequences.

    Focus on patterns:

    Once you identify a repeating pattern, determine if you need to calculate the states for all seconds individually.

    Simplify your approach:

    Assess whether storing the state codes as integers is necessary for solving the problem.

  • + 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)! }
    }