[프로그래머스][1차] 비밀지도

brick·2023년 1월 28일
0

코테

목록 보기
16/53
func solution(_ n:Int, _ arr1:[Int], _ arr2:[Int]) -> [String] {

    var answer: [String] = []

    let arr1 = arr1.map { 
        return Array(repeating: "0", count: (n - (String($0, radix: 2)).count)) + 
        String($0, radix: 2)
    }
    let arr2 = arr2.map { 
        return Array(repeating: "0", count: (n - (String($0, radix: 2)).count)) + 
        String($0, radix: 2)
    }

    for i in 0..<n {
        var arr: [String] = []
        for j in 0..<n {
            arr1[i][j] == "1" || arr2[i][j] == "1" ? arr.append("#") : arr.append(" ")
        }
        answer.append(arr.joined())
    }


    return answer
}

func solution(_ n:Int, _ arr1:[Int], _ arr2:[Int]) -> [String] {
    return (0..<n).map { String(String(arr1[$0]|arr2[$0]|2<<(n - 1), radix: 2).map { $0 == "1" ? "#" : " " }[1...n]) }
}

func solution(_ n:Int, _ arr1:[Int], _ arr2:[Int]) -> [String] {

    return (0..<n).map {
        let binary = String(arr1[$0] | arr2[$0], radix: 2)
        let padded = String(repeating: "0", count: n - binary.count) + binary
        return padded.reduce("") { $0 + ($1 == "0" ? " " : "#") }
    }
}

bit 연산자

0개의 댓글