• + 0 comments

    Nice challenge. My full solition with F# (recoursive, without local values):

    open System
    
    let padBoth c w (s : string) = s.PadLeft((w + s.Length) / 2, c).PadRight(w, c)
    
    let asciiPairs n inGap outGap =
        if n = 0 then "1" else String.Join(String.replicate outGap "_", Seq.replicate n ("1" + (String.replicate inGap "_") + "1"))
    
    let rec treeLines lvl maxDrawLvl maxLvl w cnt inPairs outPairs = seq {
        for y in [1 .. cnt] do
            yield match y with
                    | _ when lvl > maxDrawLvl -> ""
                    | _ when y <= cnt / 2 -> asciiPairs inPairs (2 * cnt - 1) (2 * cnt - 1)
                    | _                   -> asciiPairs outPairs (2 * y - cnt - 1) (3 * cnt - 2 * y - 1)
                    |> padBoth '_' w
    
        yield! if lvl >= maxLvl then Seq.empty 
            else (treeLines (lvl + 1) maxDrawLvl maxLvl w (cnt / 2) outPairs (outPairs * 2))
    }
    
    let asciiTree n w = treeLines 1 n 6 w 32 0 1 |> Seq.rev
    
    asciiTree (Console.ReadLine() |> int) 100 |> Seq.iter (printfn "%s")