Skip to content

Instantly share code, notes, and snippets.

@lukechampine
Last active October 2, 2023 03:22
Show Gist options
  • Save lukechampine/fea2f136d3c9857ad593d0a56cefa2d7 to your computer and use it in GitHub Desktop.
Save lukechampine/fea2f136d3c9857ad593d0a56cefa2d7 to your computer and use it in GitHub Desktop.
BNT renderer
package main
import (
"fmt"
)
func main() {
fmt.Println(renderBNT(19))
}
func renderBNT(n int) string {
indices := ""
var prev []int
for i := 0; i < n; i++ {
indices += fmt.Sprintf("%*d ", (i&1)*4-2, i)
prev = append(prev, i*3+(i&1))
}
rows := []string{indices}
for len(prev) > 1 {
var s []rune
row := prev[:0]
for i := 0; i < len(prev); i += 2 {
l := prev[i]
for len(s) <= l {
s = append(s, ' ')
}
if i+1 == len(prev) {
s[l] = '│'
row = append(row, l)
break
}
r := prev[i+1]
for len(s) <= r {
s = append(s, '─')
}
s[l], s[(l+r)/2], s[r] = '┌', '┴', '┐'
row = append(row, (prev[i]+prev[i+1])/2)
}
rows = append(rows, string(s))
prev = row
}
s := ""
for i := len(rows) - 1; i >= 0; i-- {
s += rows[i] + "\n"
}
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment