package main

import (
	"fmt"
	"math/rand"
	"time"
)

func bogosort(scrambled, sorted string) int {
	attempts := 0
	ch := make(chan int)

	sort_routine := func(ch chan int) {
		rand.Seed(time.Now().UnixNano())
		sort_attempt := ""
		perm := rand.Perm(len(scrambled))

		for _, rand_pos := range perm {
			sort_attempt += string(scrambled[rand_pos])
		}

		if sort_attempt == sorted {
			ch <- 1
		} else {
			ch <- 0
		}
	}

	go sort_routine(ch)
	for result := range ch {
		if result == 0 {
			attempts++
			go sort_routine(ch)
		} else {
			close(ch)
		}
	}
	return attempts
}

func main() {
	attempts := bogosort("lolhe", "hello")
	fmt.Printf("%v iterations", attempts)
}