You are viewing a single comment's thread. Return to all comments →

Instead of keeping the dimensions and replacing ones at specific positions, I reduced the whole array to half size and then worked with it.

For example, consider a part of a triangle:

___1___ __111__ _11111_ 1111111

Remove all even positions (zero based), both row- and column-wise.

_1_ 111

Put three copies of it at appropriate positions, padding with _.

_

___1___ __111__ _1___1_ 111_111

If you apply the same function again, then you get the base triangle

_1_ 1_1

and the final result

___1___ __1_1__ _1___1_ 1_1_1_1

which is exactly part of what we want.

Implementation in Haskell. The core algorithm is inside iter.

iter

main = do n <- readLn :: IO Int putStrLn $ unlines $ solve n base = [[if r + c >= 33 && c - r <= 31 then '1' else '_' | c <- [1..63]] | r <- [1..32]] dropEven s = map fst $ filter snd $ zip s $ cycle [False, True] iter s = top ++ bot where half = map dropEven $ dropEven s padding = replicate 16 '_' top = map (\x -> padding ++ x ++ padding) half bot = map (\x -> x ++ "_" ++ x) half solve n = (iterate iter base) !! n

## Functions and Fractals: Sierpinski triangles

You are viewing a single comment's thread. Return to all comments →

Instead of keeping the dimensions and replacing ones at specific positions, I reduced the whole array to half size and then worked with it.

For example, consider a part of a triangle:

Remove all even positions (zero based), both row- and column-wise.

Put three copies of it at appropriate positions, padding with

`_`

.If you apply the same function again, then you get the base triangle

and the final result

which is exactly part of what we want.

## Spoiler

Implementation in Haskell. The core algorithm is inside

`iter`

.