Skip to content

Instantly share code, notes, and snippets.

@Spirans
Created May 20, 2018 17:00
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 Spirans/2ec45cadf6cdaa10f848de8c2a9997af to your computer and use it in GitHub Desktop.
Save Spirans/2ec45cadf6cdaa10f848de8c2a9997af to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
const bf = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
func main() {
cursor := 0
arr := make([]byte, 1000)
for i := 0; i < len(bf); i++ {
switch bf[i] {
case '+': arr[cursor] += 1
case '-': arr[cursor] -= 1
case '>': cursor += 1
case '<': cursor -= 1
case '.': fmt.Printf("%c", arr[cursor])
case ',':
var input byte
_, err := fmt.Scanf("%c", &input)
if err != nil {
panic(err)
}
arr[cursor] = input
case '[':
if arr[cursor] == 0 {
for j := i; j < len(bf); j++ {
if bf[j] == byte(']') {
i = j
break
}
if j == len(bf)-1 {
panic("Could not found ']'!")
}
}
}
case ']':
if arr[cursor] != 0 {
for j := i; j > 0 ; j-- {
if bf[j] == byte('[') {
i = j
break
}
if j == 0 {
panic("Could not found '['!")
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment