40 lines
918 B
Go
40 lines
918 B
Go
//go:build !solution
|
|
|
|
package hogwarts
|
|
|
|
import (
|
|
"log"
|
|
"slices"
|
|
)
|
|
|
|
func singleCourseList(
|
|
course string, prereqs map[string][]string, list []string, traversed []string,
|
|
) []string {
|
|
if slices.Contains(traversed, course) {
|
|
log.Panic("Recursive prereqs, aborting...")
|
|
}
|
|
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
|
|
}
|
|
|
|
func GetCourseList(prereqs map[string][]string) []string {
|
|
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
|
|
}
|