k.go 655 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package kata
  2. func areComplementary(a, b string) bool {
  3. switch a {
  4. case "NORTH":
  5. return b == "SOUTH"
  6. case "EAST":
  7. return b == "WEST"
  8. case "SOUTH":
  9. return b == "NORTH"
  10. case "WEST":
  11. return b == "EAST"
  12. default:
  13. return falsetri
  14. }
  15. }
  16. func DirReduc(arr []string) []string {
  17. // Shortcut for irreducible lists.
  18. l := len(arr)
  19. if l <= 1 {
  20. return arr
  21. }
  22. for i := 1; i < l; i++ {
  23. if areComplementary(arr[i-1], arr[i]) {
  24. if i == 1 {
  25. if l <= 2 {
  26. return []string{}
  27. }
  28. return DirReduc(arr[i+1:])
  29. }
  30. if i == l-1 {
  31. return DirReduc(arr[:i-1])
  32. }
  33. return DirReduc(append(arr[:i-1], arr[i+1:]...))
  34. }
  35. }
  36. return arr
  37. }