code.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "slices"
  8. "strings"
  9. )
  10. func readInput(file *os.File) map[string][]string {
  11. scanner := bufio.NewScanner(file)
  12. computers := make(map[string][]string)
  13. for scanner.Scan() {
  14. line := scanner.Text()
  15. if line == "" {
  16. break
  17. }
  18. parts := strings.Split(line, "-")
  19. if len(parts) != 2 {
  20. log.Fatalf("Bad input: %s", line)
  21. }
  22. computers[parts[0]] = append(computers[parts[0]], parts[1])
  23. computers[parts[1]] = append(computers[parts[1]], parts[0])
  24. }
  25. return computers
  26. }
  27. func getSets(computers map[string][]string) [][]string {
  28. var sets [][]string
  29. for key, value := range computers {
  30. if key[0] != 't' {
  31. continue
  32. }
  33. for i := range value {
  34. for _, subKey := range computers[value[i]] {
  35. if subKey == key {
  36. continue
  37. }
  38. subValues := computers[subKey]
  39. for j := range subValues {
  40. if subValues[j] == key {
  41. sets = append(sets, []string{key, value[i], subKey})
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return sets
  48. }
  49. func part1(computers map[string][]string) int {
  50. sets := getSets(computers)
  51. for i := range sets {
  52. slices.Sort(sets[i])
  53. }
  54. unique := make(map[string]bool)
  55. for i := range sets {
  56. unique[strings.Join(sets[i], ",")] = true
  57. }
  58. return len(unique)
  59. }
  60. func contains(nodes []string, connections map[string]bool) bool {
  61. if len(connections) == 0 {
  62. return true
  63. }
  64. for key, _ := range connections {
  65. var isThere bool
  66. for i := range nodes {
  67. if nodes[i] == key {
  68. isThere = true
  69. break
  70. }
  71. }
  72. if !isThere {
  73. return false
  74. }
  75. }
  76. return true
  77. }
  78. func connected(key string, computers map[string][]string, connections map[string]bool) {
  79. if !contains(computers[key], connections) {
  80. return
  81. }
  82. connections[key] = true
  83. for _, value := range computers[key] {
  84. if connections[value] {
  85. continue
  86. }
  87. connected(value, computers, connections)
  88. }
  89. }
  90. func part2(computers map[string][]string) string {
  91. var allConnections []map[string]bool
  92. for key, _ := range computers {
  93. connections := make(map[string]bool)
  94. connected(key, computers, connections)
  95. allConnections = append(allConnections, connections)
  96. }
  97. var biggest int
  98. var connections map[string]bool
  99. for i := range allConnections {
  100. if len(allConnections[i]) > biggest {
  101. biggest = len(allConnections[i])
  102. connections = allConnections[i]
  103. }
  104. }
  105. var keys []string
  106. for key, _ := range connections {
  107. keys = append(keys, key)
  108. }
  109. slices.Sort(keys)
  110. return strings.Join(keys, ",")
  111. }
  112. func main() {
  113. if len(os.Args) < 2 {
  114. log.Fatal("You need to specify a file!")
  115. }
  116. filePath := os.Args[1]
  117. file, err := os.Open(filePath)
  118. if err != nil {
  119. log.Fatalf("Failed to open %s!\n", filePath)
  120. }
  121. computers := readInput(file)
  122. fmt.Println("Part1:", part1(computers))
  123. fmt.Println("Part2:", part2(computers))
  124. }