Untitled
unknown
golang
2 years ago
1.1 kB
10
Indexable
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
// Read the winning lottery numbers from standard input
winningNumbers := readNumbersFromInput()
// Initialize a map to keep track of the count of players with different matches
matchCounts := make(map[int]int)
// Read player numbers and update the matchCounts map
for i := 0; i < 10000000; i++ {
playerNumbers := readNumbersFromInput()
matches := countMatches(winningNumbers, playerNumbers)
matchCounts[matches]++
}
// Print the report
fmt.Println("Numbers matching Winners")
for i := 5; i >= 2; i-- {
fmt.Printf("%d %d\n", i, matchCounts[i])
}
}
func readNumbersFromInput() []int {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
line := scanner.Text()
nums := []int{}
for _, numStr := range bufio.NewScanner(os.Stdin).Text() {
num, _ := strconv.Atoi(numStr)
nums = append(nums, num)
}
return nums
}
func countMatches(winningNumbers, playerNumbers []int) int {
matches := 0
for _, wn := range winningNumbers {
for _, pn := range playerNumbers {
if wn == pn {
matches++
break
}
}
}
return matches
}
Editor is loading...