code.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strings"
  8. "unicode"
  9. )
  10. func readInput(file string) map[string][]string {
  11. content, err := ioutil.ReadFile(file)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. lines := strings.Split(string(content), "\n")
  16. input := make(map[string][]string)
  17. for _, line := range lines {
  18. if line == "" {
  19. continue
  20. }
  21. points := strings.Split(line, "-")
  22. if len(points) != 2 {
  23. log.Fatal("Invalid input")
  24. }
  25. input[points[0]] = append(input[points[0]], points[1])
  26. input[points[1]] = append(input[points[1]], points[0])
  27. }
  28. return input
  29. }
  30. var paths [][]string
  31. func findAllPaths(start string, end string, visited map[string]bool, input map[string][]string, localPath []string) {
  32. if start == end {
  33. paths = append(paths, localPath)
  34. return
  35. }
  36. if unicode.IsLower(rune(start[0])) {
  37. visited[start] = true
  38. }
  39. for _, next := range input[start] {
  40. if !visited[next] {
  41. localPath = append(localPath, next)
  42. findAllPaths(next, end, visited, input, localPath)
  43. localPath = localPath[:len(localPath)-1]
  44. }
  45. }
  46. if visited[start] {
  47. visited[start] = false
  48. }
  49. }
  50. func part1(input map[string][]string) int {
  51. visited := make(map[string]bool)
  52. localPath := []string{"start"}
  53. findAllPaths("start", "end", visited, input, localPath)
  54. return len(paths)
  55. }
  56. func anyVisitedTwice(visited map[string]int) bool {
  57. for _, v := range visited {
  58. if v > 1 {
  59. return true
  60. }
  61. }
  62. return false
  63. }
  64. func findAllPathsPart2(start string, end string, visited map[string]int, input map[string][]string, localPath []string) {
  65. if start == end {
  66. paths = append(paths, localPath)
  67. return
  68. }
  69. if unicode.IsLower(rune(start[0])) {
  70. visited[start]++
  71. }
  72. for _, next := range input[start] {
  73. if next != "start" {
  74. if visited[next]+1 > 1 {
  75. if anyVisitedTwice(visited) {
  76. continue
  77. }
  78. }
  79. localPath = append(localPath, next)
  80. findAllPathsPart2(next, end, visited, input, localPath)
  81. localPath = localPath[:len(localPath)-1]
  82. }
  83. }
  84. visited[start]--
  85. }
  86. func part2(input map[string][]string) int {
  87. visited := make(map[string]int)
  88. localPath := []string{"start"}
  89. paths = make([][]string, 0)
  90. findAllPathsPart2("start", "end", visited, input, localPath)
  91. return len(paths)
  92. }
  93. func main() {
  94. if len(os.Args) < 2 {
  95. log.Fatal("Please provide a file name as argument")
  96. }
  97. input := readInput(os.Args[1])
  98. fmt.Println("Part 1:", part1(input))
  99. fmt.Println("Part 2:", part2(input))
  100. }