|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +//go:embed input.txt |
| 11 | +var input string |
| 12 | + |
| 13 | +func main() { |
| 14 | + inputLines := strings.Split(input, "\n") |
| 15 | + |
| 16 | + graph := make(map[string][]string) |
| 17 | + for _, line := range inputLines { |
| 18 | + if line == "" { |
| 19 | + continue |
| 20 | + } |
| 21 | + elements := strings.Split(line, "-") |
| 22 | + graph[elements[0]] = append(graph[elements[0]], elements[1]) |
| 23 | + graph[elements[1]] = append(graph[elements[1]], elements[0]) |
| 24 | + } |
| 25 | + |
| 26 | + fmt.Println("Solving \"Day 23: LAN Party\"...") |
| 27 | + fmt.Println("Part 1 result:", partOne(graph)) |
| 28 | + fmt.Println("Part 2 result:", partTwo(graph)) |
| 29 | +} |
| 30 | + |
| 31 | +func partOne(graph map[string][]string) int { |
| 32 | + count := 0 |
| 33 | + |
| 34 | + nodes := make([]string, 0, len(graph)) |
| 35 | + for node := range graph { |
| 36 | + nodes = append(nodes, node) |
| 37 | + } |
| 38 | + |
| 39 | + for i := 0; i < len(nodes); i++ { |
| 40 | + a := nodes[i] |
| 41 | + for j := i + 1; j < len(nodes); j++ { |
| 42 | + b := nodes[j] |
| 43 | + for k := j + 1; k < len(nodes); k++ { |
| 44 | + c := nodes[k] |
| 45 | + if isConnected(graph, a, b) && isConnected(graph, a, c) && isConnected(graph, b, c) { |
| 46 | + if strings.HasPrefix(a, "t") || strings.HasPrefix(b, "t") || strings.HasPrefix(c, "t") { |
| 47 | + count++ |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return count |
| 55 | +} |
| 56 | + |
| 57 | +func partTwo(graph map[string][]string) string { |
| 58 | + var maxClique []string |
| 59 | + |
| 60 | + nodes := make([]string, 0, len(graph)) |
| 61 | + for node := range graph { |
| 62 | + nodes = append(nodes, node) |
| 63 | + } |
| 64 | + |
| 65 | + bronKerbosch([]string{}, nodes, []string{}, graph, &maxClique) |
| 66 | + |
| 67 | + sort.Strings(maxClique) |
| 68 | + password := strings.Join(maxClique, ",") |
| 69 | + |
| 70 | + return password |
| 71 | +} |
| 72 | + |
| 73 | +func bronKerbosch(r, p, x []string, graph map[string][]string, maxClique *[]string) { |
| 74 | + if len(p) == 0 && len(x) == 0 { |
| 75 | + if len(r) > len(*maxClique) { |
| 76 | + *maxClique = append([]string{}, r...) |
| 77 | + } |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + for i := 0; i < len(p); i++ { |
| 82 | + v := p[i] |
| 83 | + bronKerbosch( |
| 84 | + append(r, v), |
| 85 | + intersect(p, graph[v]), |
| 86 | + intersect(x, graph[v]), |
| 87 | + graph, |
| 88 | + maxClique, |
| 89 | + ) |
| 90 | + p = append(p[:i], p[i+1:]...) |
| 91 | + x = append(x, v) |
| 92 | + i-- |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func isConnected(graph map[string][]string, node1, node2 string) bool { |
| 97 | + for _, neighbor := range graph[node1] { |
| 98 | + if neighbor == node2 { |
| 99 | + return true |
| 100 | + } |
| 101 | + } |
| 102 | + return false |
| 103 | +} |
| 104 | + |
| 105 | +func intersect(a, b []string) []string { |
| 106 | + set := make(map[string]bool) |
| 107 | + for _, v := range b { |
| 108 | + set[v] = true |
| 109 | + } |
| 110 | + |
| 111 | + var result []string |
| 112 | + for _, v := range a { |
| 113 | + if set[v] { |
| 114 | + result = append(result, v) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return result |
| 119 | +} |
0 commit comments