123456789101112131415161718192021222324252627282930313233343536373839 |
- package kata
- func areComplementary(a, b string) bool {
- switch a {
- case "NORTH":
- return b == "SOUTH"
- case "EAST":
- return b == "WEST"
- case "SOUTH":
- return b == "NORTH"
- case "WEST":
- return b == "EAST"
- default:
- return falsetri
- }
- }
- func DirReduc(arr []string) []string {
- // Shortcut for irreducible lists.
- l := len(arr)
- if l <= 1 {
- return arr
- }
- for i := 1; i < l; i++ {
- if areComplementary(arr[i-1], arr[i]) {
- if i == 1 {
- if l <= 2 {
- return []string{}
- }
- return DirReduc(arr[i+1:])
- }
- if i == l-1 {
- return DirReduc(arr[:i-1])
- }
- return DirReduc(append(arr[:i-1], arr[i+1:]...))
- }
- }
- return arr
- }
|