|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/ayo-awe/advent-of-code-2024/aoc" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + input, err := aoc.ReadInputLineByLine("input.txt") |
| 14 | + if err != nil { |
| 15 | + log.Fatal(err) |
| 16 | + } |
| 17 | + |
| 18 | + network := buildNetwork(input) |
| 19 | + |
| 20 | + fmt.Println("solution to part one: ", PartOne(network)) |
| 21 | + fmt.Println("solution to part two: ", PartTwo()) |
| 22 | +} |
| 23 | + |
| 24 | +func PartOne(network map[string]map[string]struct{}) int { |
| 25 | + matches := make(map[string]struct{}) |
| 26 | + for node, subnet := range network { |
| 27 | + if !strings.HasPrefix(node, "t") { |
| 28 | + continue |
| 29 | + } |
| 30 | + |
| 31 | + var subnodes []string |
| 32 | + for subnode := range subnet { |
| 33 | + subnodes = append(subnodes, subnode) |
| 34 | + } |
| 35 | + |
| 36 | + for i := 0; i < len(subnodes)-1; i++ { |
| 37 | + for j := i + 1; j < len(subnodes); j++ { |
| 38 | + subA, subB := subnodes[i], subnodes[j] |
| 39 | + lookupKey := []string{node, subA, subB} |
| 40 | + sort.Strings(lookupKey) |
| 41 | + |
| 42 | + key := strings.Join(lookupKey, ",") |
| 43 | + |
| 44 | + if _, exists := matches[key]; exists { |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + if _, connected := network[subA][subB]; connected { |
| 49 | + matches[key] = struct{}{} |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return len(matches) |
| 56 | +} |
| 57 | + |
| 58 | +func PartTwo(network map[string]map[string]struct{}) string { |
| 59 | + matches := make(map[string]struct{}) |
| 60 | + for node, subnet := range network { |
| 61 | + if !strings.HasPrefix(node, "t") { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + var subnodes []string |
| 66 | + for subnode := range subnet { |
| 67 | + subnodes = append(subnodes, subnode) |
| 68 | + } |
| 69 | + |
| 70 | + for i := 0; i < len(subnodes)-1; i++ { |
| 71 | + for j := i + 1; j < len(subnodes); j++ { |
| 72 | + subA, subB := subnodes[i], subnodes[j] |
| 73 | + lookupKey := []string{node, subA, subB} |
| 74 | + sort.Strings(lookupKey) |
| 75 | + |
| 76 | + key := strings.Join(lookupKey, ",") |
| 77 | + |
| 78 | + if _, exists := matches[key]; exists { |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + if _, connected := network[subA][subB]; connected { |
| 83 | + matches[key] = struct{}{} |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return 0 |
| 90 | +} |
| 91 | + |
| 92 | +func buildNetwork(connections []string) map[string]map[string]struct{} { |
| 93 | + network := make(map[string]map[string]struct{}) |
| 94 | + for _, connection := range connections { |
| 95 | + nodes := strings.Split(connection, "-") |
| 96 | + node1, node2 := nodes[0], nodes[1] |
| 97 | + |
| 98 | + if _, ok := network[node1]; !ok { |
| 99 | + network[node1] = make(map[string]struct{}) |
| 100 | + } |
| 101 | + |
| 102 | + if _, ok := network[node2]; !ok { |
| 103 | + network[node2] = make(map[string]struct{}) |
| 104 | + } |
| 105 | + |
| 106 | + network[node1][node2] = struct{}{} |
| 107 | + network[node2][node1] = struct{}{} |
| 108 | + } |
| 109 | + return network |
| 110 | +} |
0 commit comments