Skip to content

Instantly share code, notes, and snippets.

@caelifer
Last active July 28, 2020 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caelifer/85b10ad1a593218a60d2d8c871826560 to your computer and use it in GitHub Desktop.
Save caelifer/85b10ad1a593218a60d2d8c871826560 to your computer and use it in GitHub Desktop.
// Go implementation for the "Functions and Fractals - Recursive Trees - Bash!" problem.
// https://www.hackerrank.com/challenges/fractal-trees-all/problem
package main
import (
"fmt"
"strings"
)
const h = 15 // half-heigth of the Y pattern
func main() {
var scrn = []string{}
for z := uint64(0); z < 6; z++ {
x := uint64(1 << z)
for i := uint64(0); i < h/x+1; i++ {
scrn = addLine(scrn, stem(x), x)
}
for i := uint64(0); i < h/x+1; i++ {
scrn = addLine(scrn, fork(x, i), x)
}
}
printScreen(scrn)
}
func stem(scale uint64) uint64 {
return 1 << (64/(2*scale) - 1)
}
func fork(scale, iter uint64) uint64 {
n := uint64((1 << (2 * iter)) | 1)
return n << (64/(2*scale) - iter - 1)
}
func addLine(slice []string, seg, scale uint64) []string {
n, ln := uint64(0), 64/scale
for i := uint64(0); i < scale; i++ {
n |= seg << (i * ln)
}
line := strings.ReplaceAll(fmt.Sprintf("%064b", n), "0", "_")
return append(slice, line)
}
func printScreen(scrn []string) {
fmt.Println("____________________________________________________________________________________________________")
for i := len(scrn) - 2; i > 0; i-- {
fmt.Printf("%s%s%s\n", "_________________", scrn[i], "___________________")
}
}
@caelifer
Copy link
Author

caelifer commented Jul 28, 2020

Live code - Live code - https://play.golang.org/p/qXfP_6i7AMW

Output:

____________________________________________________________________________________________________
__________________1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1___________________
___________________1___1___1___1___1___1___1___1___1___1___1___1___1___1___1___1____________________
___________________1___1___1___1___1___1___1___1___1___1___1___1___1___1___1___1____________________
____________________1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____________________
_____________________1_______1_______1_______1_______1_______1_______1_______1______________________
_____________________1_______1_______1_______1_______1_______1_______1_______1______________________
_____________________1_______1_______1_______1_______1_______1_______1_______1______________________
______________________1_____1_________1_____1_________1_____1_________1_____1_______________________
_______________________1___1___________1___1___________1___1___________1___1________________________
________________________1_1_____________1_1_____________1_1_____________1_1_________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
__________________________1_____________1_________________1_____________1___________________________
___________________________1___________1___________________1___________1____________________________
____________________________1_________1_____________________1_________1_____________________________
_____________________________1_______1_______________________1_______1______________________________
______________________________1_____1_________________________1_____1_______________________________
_______________________________1___1___________________________1___1________________________________
________________________________1_1_____________________________1_1_________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
__________________________________1_____________________________1___________________________________
___________________________________1___________________________1____________________________________
____________________________________1_________________________1_____________________________________
_____________________________________1_______________________1______________________________________
______________________________________1_____________________1_______________________________________
_______________________________________1___________________1________________________________________
________________________________________1_________________1_________________________________________
_________________________________________1_______________1__________________________________________
__________________________________________1_____________1___________________________________________
___________________________________________1___________1____________________________________________
____________________________________________1_________1_____________________________________________
_____________________________________________1_______1______________________________________________
______________________________________________1_____1_______________________________________________
_______________________________________________1___1________________________________________________
________________________________________________1_1_________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment