shad-go/hogwarts/courselist.go

41 lines
918 B
Go
Raw Normal View History

2022-02-10 22:06:57 +00:00
//go:build !solution
2020-02-24 19:54:48 +00:00
package hogwarts
2024-06-02 23:06:06 +00:00
import (
"log"
"slices"
)
func singleCourseList(
course string, prereqs map[string][]string, list []string, traversed []string,
) []string {
if slices.Contains(traversed, course) {
2024-06-05 23:15:25 +00:00
log.Panic("recursive prereqs, aborting...")
2024-06-02 23:06:06 +00:00
}
traversed = append(traversed, course)
for _, prereqCourse := range prereqs[course] {
if !slices.Contains(list, prereqCourse) {
i := slices.Index(list, course)
if i == -1 {
list = append(list, prereqCourse)
} else {
list = slices.Insert(list, i, prereqCourse)
}
}
list = singleCourseList(prereqCourse, prereqs, list, traversed)
}
return list
}
2020-02-24 19:54:48 +00:00
func GetCourseList(prereqs map[string][]string) []string {
2024-06-02 23:06:06 +00:00
list := make([]string, 0)
for course := range prereqs {
if !slices.Contains(list, course) {
list = append(list, course)
}
list = singleCourseList(course, prereqs, list, []string{})
}
return list
2020-02-24 19:54:48 +00:00
}