CtCI - Triforce Secret Realm

Link has been sent to the Secret Realm to find the Triforce and save Hyrule. But the vile Ganondorf followed Link into the "Temple of Time", trapped him, and took the Triforce for himself. The "Temple of Time" has been closed for a thousand years now, but the Sages finally found you and they need your help to open the Temple of Time's door and join Link in the ultimate battle.

The Temple of Time's surface contains several incrusted Triforces of different sizes, and the Sages believe that by creating Triforces of the corresponding sizes, the door will open. Even though no magic has worked until now, your programming skills will surely make the difference.

You must create a program that echoes a Triforce of a given size N.

  • A triforce is made of three identical triangles,
  • A triangle of size N should be made of N lines,
  • A triangle's line starts from one symbol, and earns two symbols each line,
  • Take care, a dot must be located at the top/left to avoid automatic trimming.
.    @
    @@@
   @@@@@
  @     @
 @@@   @@@
@@@@@ @@@@@

For a Triforce with N=5:

.        @
        @@@
       @@@@@
      @@@@@@@
     @@@@@@@@@
    @         @
   @@@       @@@
  @@@@@     @@@@@
 @@@@@@@   @@@@@@@
@@@@@@@@@ @@@@@@@@@

We already have part of the solution given in the specifications of the problem. Each triangle is built with the same height as the input N and for each line we add two additional symbols starting with one in the first line.

var size int = 5

var spaces int
var line []byte
var count int = 1
var char = []byte("@")

for y := 1; y <= size; y++ {
    line = []byte{}
    spaces = size - y
    line = append(line, bytes.Repeat([]byte("\x20"), spaces)...)
    line = append(line, bytes.Repeat(char, count)...)
    line = append(line, bytes.Repeat([]byte("\x20"), spaces)...)
    fmt.Printf("%s\n", line)
    count += 2
}
Output:
|    @    |
|   @@@   |
|  @@@@@  |
| @@@@@@@ |
|@@@@@@@@@|

The second section of the Trifoce contains two triangles separated by a vertical white space. The generation of each figure follows the same rules as the previous triangle so there is no modification here except that each line will contain double the number of characters.

line = append(line, bytes.Repeat([]byte("\x20"), spaces)...)
line = append(line, bytes.Repeat(char, count)...)
line = append(line, bytes.Repeat([]byte("\x20"), spaces)...)

line = append(line, '\x20') /* Vertical Padding */

line = append(line, bytes.Repeat([]byte("\x20"), spaces)...)
line = append(line, bytes.Repeat(char, count)...)
line = append(line, bytes.Repeat([]byte("\x20"), spaces)...)

Notice that we simply duplicated the code that fills the array of characters with a modification in the number of spaces on the right side of the triangle on the left. This additional space on each line will generate the vertical padding that we need to separate the two figures.

|    @    |
|   @@@   |
|  @@@@@  |
| @@@@@@@ |
|@@@@@@@@@|
|    @         @    |
|   @@@       @@@   |
|  @@@@@     @@@@@  |
| @@@@@@@   @@@@@@@ |
|@@@@@@@@@ @@@@@@@@@|

Now we need to center the first triangle by adding the same amount of white spaces on the sides. Visually moving the triangle to the right we find that the required number of spaces for the base is equal to the input N and for each previous line we just need to add one. But since we are going to print these triangle from top to bottom we have to calculate the number of required spaces from y=0 to y=N.

|555554444.444455555| Y=1; S=N+(N-Y); S=5+(5-1) => 4
|55555333...33355555| Y=2; S=N+(N-Y); S=5+(5-2) => 3
|5555522.....2255555| Y=3; S=N+(N-Y); S=5+(5-3) => 2
|555551.......155555| Y=4; S=N+(N-Y); S=5+(5-4) => 1
|55555.........55555| Y=5; S=N+(N-Y); S=5+(5-5) => 0

We change spaces=(size-y) to spaces=(size+(size-y)) for the first triangle.

Now for the final touch, we replace position X=0, Y=0 with a single dot.

To simplify, we can remove the character lists and print directly to I/O:

func solution(size int) {
    var count int = 1
    var spaces int = size + (size - 1)

    fmt.Println("." +
        strings.Repeat("\x20", spaces-1) +
        strings.Repeat("@", count) +
        strings.Repeat("\x20", spaces))
    count += 2

    for y := 2; y <= size; y++ {
        spaces = size + (size - y)
        fmt.Println(strings.Repeat("\x20", spaces) +
            strings.Repeat("@", count) +
            strings.Repeat("\x20", spaces))
        count += 2
    }

    count = 1

    for y := 1; y <= size; y++ {
        spaces = size - y
        fmt.Print(strings.Repeat("\x20", spaces) +
            strings.Repeat("@", count) +
            strings.Repeat("\x20", spaces+1))
        fmt.Println(strings.Repeat("\x20", spaces) +
            strings.Repeat("@", count) +
            strings.Repeat("\x20", spaces))
        count += 2
    }
}
7 months ago
  • 8da57acFix minor bugs found by the code static ana…
7 months ago
  • caa417fAdd option to configure the malware scanner…
7 months ago
  • 9c86744Modify default value for some of the alert …
7 months ago
  • 84dd39dAdd option to stop sending the failed login…
7 months ago
  • eb05935Add pre-checks for every plugin page for si…
7 months ago
  • d21a062Modify mechanism to ignore files from integ…
7 months ago
  • b1a9169Add developer option to disable failed pass…
7 months ago
  • 4e3ef13Add support for other English and Spanish b…
7 months ago
  • 2d07b4eFix error interception for Firewall API err…
7 months ago
pushed to master at cixtor/slackapi
  • 4a2c1c8Modify data type for methods related to cha…
  • 6716199Add CLI handler for the users.identity API …
  • f9c448dAdd CLI handler for the mpim.open API endpo…
  • 305d1c4Add CLI handler for the mpim.mark API endpo…
  • 8bb89afAdd CLI handler for the mpim.close API endp…
  • 202a017Add CLI handler for the dnd.teamInfo API en…
  • 68819e9Add CLI handler for the dnd.info API endpoi…
  • 9a2b29aAdd CLI handler for the dnd.endSnooze API e…
  • e7dc86aAdd CLI handler for the dnd.setSnooze API e…
  • 111c53fAdd CLI handler for the dnd.endDnd API endp…
  • cdb620dFix token usage when there are no extra par…
  • View comparison for these 11 commits
7 months ago
pushed to master at cixtor/slackapi
7 months ago
  • 6497e80Remove unnecessary automatic blacklisting o…
7 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#40
Fix multiple bugs with the API calls and queue system
18 commit with 793 additions and 293 deletion
7 months ago
7 months ago
7 months ago
  • 38cc02aModify timing for the dashboard alerts afte…
7 months ago
  • 350c074Fix infinite loop with email alerts and SMT…
7 months ago
  • acff4aaFix detection of base URL with built-in fun…
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#39
Add queue system for the security logs and cache improvement
5 commit with 517 additions and 541 deletion
7 months ago
7 months ago
  • 4c51445Fix static function call of non-static Site…
7 months ago
7 months ago
  • af47581Add changelog to release version 1.8.6
7 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#38
Add changelog to release version 1.8.5
6 commit with 4062 additions and 1841 deletion
7 months ago
  • dc1a05aAdd changelog to release version 1.8.5
7 months ago
Do you have a project idea? Let's make it together!