|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "slices" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type Connection struct { |
| 12 | + pc1 string |
| 13 | + pc2 string |
| 14 | +} |
| 15 | +type Computer struct { |
| 16 | + name string |
| 17 | + connectedComputers []string |
| 18 | + fullyConnectedComputerSets [][]string |
| 19 | +} |
| 20 | + |
| 21 | +var computers map[string]Computer |
| 22 | + |
| 23 | +func main() { |
| 24 | + filenames := []string{"testInput", "testInput2", "input"} |
| 25 | + for _, fileName := range filenames { |
| 26 | + fileContents, err := os.ReadFile(fileName) |
| 27 | + if err != nil { |
| 28 | + panic(err) |
| 29 | + } |
| 30 | + fmt.Println(fileName) |
| 31 | + lines := strings.Split(string(fileContents), "\n") |
| 32 | + connections := make([]Connection, len(lines)) |
| 33 | + for index, line := range lines { |
| 34 | + connections[index] = Connection{line[0:2], line[3:]} |
| 35 | + } |
| 36 | + computers = make(map[string]Computer) |
| 37 | + for _, connection := range connections { |
| 38 | + pc1, ok := computers[connection.pc1] |
| 39 | + if ok { |
| 40 | + if !slices.Contains(computers[connection.pc1].connectedComputers, connection.pc2) { |
| 41 | + pc1.connectedComputers = append(pc1.connectedComputers, connection.pc2) |
| 42 | + computers[connection.pc1] = pc1 |
| 43 | + } |
| 44 | + } else { |
| 45 | + computers[connection.pc1] = Computer{name: connection.pc1, connectedComputers: []string{connection.pc2}} |
| 46 | + } |
| 47 | + pc2, ok := computers[connection.pc2] |
| 48 | + if ok { |
| 49 | + if !slices.Contains(computers[connection.pc2].connectedComputers, connection.pc1) { |
| 50 | + pc2.connectedComputers = append(pc2.connectedComputers, connection.pc1) |
| 51 | + computers[connection.pc2] = pc2 |
| 52 | + } |
| 53 | + } else { |
| 54 | + computers[connection.pc2] = Computer{name: connection.pc2, connectedComputers: []string{connection.pc1}} |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + for index, computer := range computers { |
| 59 | + //println("Processing " + index) |
| 60 | + computer.fullyConnectedComputerSets = findFullyConnectedSets(computer.connectedComputers) |
| 61 | + computers[index] = computer |
| 62 | + } |
| 63 | + maxConnections := 0 |
| 64 | + var mostConnectedSet []string |
| 65 | + for _, computer := range computers { |
| 66 | + for _, set := range computer.fullyConnectedComputerSets { |
| 67 | + if len(set) > maxConnections { |
| 68 | + maxConnections = len(set) |
| 69 | + mostConnectedSet = append(set, computer.name) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + sort.Strings(mostConnectedSet) |
| 74 | + println(maxConnections) |
| 75 | + println(strings.Join(mostConnectedSet, ",")) |
| 76 | + } |
| 77 | +} |
| 78 | +func findFullyConnectedSets(set []string) [][]string { |
| 79 | + if len(set) < 2 { |
| 80 | + return make([][]string, 0) |
| 81 | + } |
| 82 | + if isSetFullyConnected(set) { |
| 83 | + return [][]string{set} |
| 84 | + } |
| 85 | + sets := make([][]string, 0) |
| 86 | + for index := range set { |
| 87 | + excluded := make([]string, len(set)) |
| 88 | + copy(excluded, set) |
| 89 | + excluded = append(excluded[:index], excluded[index+1:]...) |
| 90 | + fullyConnected := findFullyConnectedSets(excluded) |
| 91 | + if len(fullyConnected) > 0 { |
| 92 | + return fullyConnected |
| 93 | + } |
| 94 | + } |
| 95 | + slices.CompactFunc(sets, func(i []string, i2 []string) bool { |
| 96 | + for _, el := range i { |
| 97 | + if slices.Contains(i2, el) { |
| 98 | + return true |
| 99 | + } |
| 100 | + } |
| 101 | + return false |
| 102 | + }) |
| 103 | + |
| 104 | + return sets |
| 105 | +} |
| 106 | +func isSetFullyConnected(set []string) bool { |
| 107 | + for _, computerName := range set { |
| 108 | + computer := computers[computerName] |
| 109 | + for _, computerName2 := range set { |
| 110 | + if computer.name == computerName2 { |
| 111 | + continue |
| 112 | + } |
| 113 | + if !slices.Contains(computer.connectedComputers, computerName2) { |
| 114 | + return false |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return true |
| 119 | +} |
0 commit comments