1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| package main
import ( "fmt" "math/rand" "time" )
func contains(needle int, haystack []int) bool { for _, ele := range haystack { if needle == ele { return true } }
return false }
func random(number int) []int { var numbers []int
rand.Seed(time.Now().Unix())
i := 0 for i < number { number := rand.Intn(10)
if number > 0 && !contains(number, numbers) { numbers = append(numbers, number) i++ } }
return numbers }
func explode(number int) []int { var array []int
for number >= 1 { array = append([]int{number % 10}, array...)
number /= 10 }
return array }
func compare(expected []int, actual []int) (int, int) { var a int var b int
for expectedIndex, expectedValue := range expected { for actualIndex, actualValue := range actual { if expectedValue == actualValue { if expectedIndex == actualIndex { a++ break } b++ } } }
return a, b }
func main() { digits := 4
actual := random(digits)
var input int
for { fmt.Scan(&input)
expected := explode(input)
if len(expected) != digits { fmt.Println("Invalid input") continue }
a, b := compare(expected, actual)
if a == digits { break }
fmt.Printf("%dA%dB\n", a, b) } }
|