package main
 
import (
	"cmp"
	"io"
	"log"
	"maps"
	"os"
	"slices"
)
 
type Team struct {
	Name        string
	MemberNames []string
}
 
type League struct {
	Teams []Team
	Wins  map[string]int
}
 
type Ranker interface {
	Ranking() []string
}
 
func (l *League) MatchResult(team1 string, score1 int, team2 string, score2 int) {
	if score1 > score2 {
		l.Wins[team1]++
	} else if score2 > score1 {
		l.Wins[team2]++
	}
}
 
func (l *League) Ranking() []string {
	sortedNames := slices.SortedFunc(maps.Keys(l.Wins), func(a, b string) int {
		return cmp.Compare(l.Wins[b], l.Wins[a])
	})
	return sortedNames
}
 
func RankPrinter(r Ranker, w io.Writer) {
	for _, name := range r.Ranking() {
		if _, err := io.WriteString(w, name+"\n"); err != nil {
			log.Fatal(err)
		}
	}
}
 
func main() {
	t1 := Team{Name: "A", MemberNames: []string{"a", "b", "c"}}
	t2 := Team{Name: "B", MemberNames: []string{"d", "e", "f"}}
 
	l := League{
		Teams: []Team{t1, t2},
		Wins:  map[string]int{},
	}
 
	l.MatchResult("A", 3, "B", 1)
	l.MatchResult("A", 2, "B", 0)
	l.MatchResult("B", 4, "A", 1)
 
	RankPrinter(&l, os.Stdout)
}