Sort by

recency

|

16 Discussions

|

  • + 0 comments

    Can be super concise in Haskell!

    splitNodeAt j seglen = map (\x -> [j - x, j + x]) [1..seglen]
    
    treeColumns _ 0 _ = [[]]
    treeColumns j segLen d = 
        (if d > 0
            then replicate segLen [j] ++ splitNodeAt j segLen
            else replicate (2 * segLen) [])
        ++ zipWith (++) (treeColumns (j - segLen) (segLen `div` 2) (d - 1))
    		                          (treeColumns (j + segLen) (segLen `div` 2) (d - 1))
    
    generateLine len indices = map (\i -> if i `elem` indices then '1' else '_') [0..len-1]
    
    main = getLine >>= (putStrLn . unlines . reverse . map (generateLine 100) . treeColumns 49 16 . read)
    
  • + 0 comments

    My Haskell solution.

    It's a bit cheeky but the idea is straight-forward:

    1) Find the tree stem roots 2) Add stem length at roots 3) Make branches at top of each root

    Not elegant but it gets the job done.

    totalColumns = 100
    totalRows = 63
    emptySpace = '_'
    filledSpace = '1'
    centralStemIndex = 49
    firstBranchHeight = 16
    firstBranchOffset = 32
    maxIterations = 5
    
    getTotalEmptyRows :: Int -> Int
    getTotalEmptyRows iterations =
      foldl (\acc e -> acc + (2 ^ e) ) 1 [1..(maxIterations - iterations)]
    
    getIterationStemHeight :: Int -> Int
    getIterationStemHeight iteration = 2 ^ (maxIterations - iteration)
    
    generateEmptySpace :: Int -> [Char]
    generateEmptySpace width = replicate width emptySpace
    
    generateRow :: [Int] -> [Char]
    generateRow spacesToFill =
      foldl (\acc e -> if e `elem` spacesToFill
        then acc ++ [filledSpace] else acc ++ [emptySpace]) "" [0..99]
    
    createIterationOffsetIndex :: Int -> Int
    createIterationOffsetIndex 1 = 0
    createIterationOffsetIndex iteration =
      foldl (\acc e -> acc + ((2 ^ (maxIterations - e)))) 0 [1 .. iteration - 1]
    
    getStemLocationsForCurrentIteration :: Int -> [Int]
    getStemLocationsForCurrentIteration iteration =
      let totalStems = 2 ^ (iteration - 1)
          stemOffsets = (2 * firstBranchOffset) `div` (2 ^ (iteration - 1))
          firstIndexOffset = (centralStemIndex - createIterationOffsetIndex iteration)
      in zipWith (\x y -> x + stemOffsets * y) (replicate totalStems firstIndexOffset) [0..]
    
    generateYStem :: Int -> [[Char]]
    generateYStem iteration =
      let stemHeight = getIterationStemHeight iteration 
          stemLocations = getStemLocationsForCurrentIteration iteration
      in replicate stemHeight (generateRow stemLocations)
    
    generateYBranches :: Int -> [[Char]]
    generateYBranches iteration =
      let stemHeight = getIterationStemHeight iteration
          rootPoints = getStemLocationsForCurrentIteration iteration
          completeMatrix = foldl (\acc e -> acc ++ [(map (\x -> [x - e, x + e]) rootPoints)]) [] [1..stemHeight]
      in map generateRow $ map concat (reverse completeMatrix)
      
    
    generateYForIteration :: Int -> [[Char]]
    generateYForIteration iteration =
      let stems = generateYStem iteration
          branches = generateYBranches iteration
      in branches ++ stems
    
    generateFractal :: Int -> [[Char]]
    generateFractal iterations =
       concat $ map (\i -> generateYForIteration i) (reverse [1.. iterations])
    
    main = do
      input <- getContents
      let n = read input :: Int
          emptyRows = getTotalEmptyRows n
          emptyRowMatrix = foldl (\acc e -> acc ++ [(generateRow [])]) [] [1..emptyRows]
      putStr $ unlines $ emptyRowMatrix
      putStr $ unlines $ generateFractal n
    
  • + 0 comments

    My solution in scala. Because of restriction in not using local variables, some computations were repeated.

    import scala.io.StdIn
    import scala.annotation.tailrec
    
    object Solution {
    
      def drawTrees(n: Int): Unit = {
        def recursiveTrees(n: Int, pHeight: Int, root: Int, base: Int, arr: Array[Array[Char]]): Array[Array[Char]] = {
          if (n > 0) {
            for (i <- Range(base, base-pHeight, -1)) {
              arr(i)(root) = '1'
            }
            @tailrec
            def fill_branch(i: Int, j: Int, x:Int, counter: Int): Unit = {
              if (counter != 0) {
                arr(x)(i) = '1'
                arr(x)(j) = '1'
                fill_branch(i-1, j+1, x-1, counter-1)
              }
            }
            fill_branch(root-1, root+1, base-pHeight, pHeight)
            recursiveTrees(n-1, pHeight/2, root-pHeight, base-(pHeight*2), arr)
            recursiveTrees(n-1, pHeight/2, root+pHeight, base-(pHeight*2), arr)
          }
          return arr
        }
        recursiveTrees(n, 16, 49, 62, Array.fill(63, 100)('_')).foreach(row => println(row.mkString))
      }
    
      def main(args: Array[String]) {
          drawTrees(StdIn.readInt())
      }
    }
    
  • + 0 comments

    I gotta say, this person did a magnificently bad job of making it symmetrical

  • + 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")