Last active
March 3, 2020 07:19
-
-
Save yusuke024/a7316a02ff4c2797ed753e399a9dfef6 to your computer and use it in GitHub Desktop.
A in A
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env swift | |
// swift aina.swift <n> | |
import Foundation | |
func p(_ n: Int) -> [String] { | |
if n == 0 { return ["A"] } | |
let p1 = [" A ", "A A", "AAA", "A A", "A A"] | |
var result: [String] = [] | |
let prev = p(n-1) // Recursively get result of P(n-1). | |
for line in prev { | |
for p1l in p1 { // For each line in P(1) | |
var l = "" | |
for c in line { // Iterate each character in previous result | |
if c == " " { l += " " } // 3 spaces. Because P(1) is 3-space wide. | |
if c == "A" { l += p1l } | |
} | |
result.append(l) | |
} | |
} | |
return result | |
} | |
guard CommandLine.arguments.count == 2, let n = Int(CommandLine.arguments[1]) else { | |
fatalError("Need one argument (Int)") | |
} | |
print(p(n).joined(separator: "\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
P(3)