|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// Each bind has exactly two ends. Their ordering doesn't matter. |
| 12 | +type Bind struct { |
| 13 | + id int |
| 14 | + End1 int |
| 15 | + End2 int |
| 16 | +} |
| 17 | + |
| 18 | +func main() { |
| 19 | + binds := createBinds() |
| 20 | + // Start the search from port type 0 |
| 21 | + bridges := [][]Bind{} |
| 22 | + used := make([]bool, len(binds)) |
| 23 | + findBridges(0, binds, used, []Bind{}, &bridges) |
| 24 | + maximum := 0 |
| 25 | + for _, bridge := range bridges { |
| 26 | + // for _, b := range bridge { |
| 27 | + // fmt.Printf("%d/%d -> ", b.End1, b.End2) |
| 28 | + // } |
| 29 | + str := calculateStrength(bridge) |
| 30 | + if str > maximum { |
| 31 | + maximum = str |
| 32 | + } |
| 33 | + } |
| 34 | + fmt.Println("Part 1 | Max Strength is: ", maximum) |
| 35 | + longest := longestSlice(bridges) |
| 36 | + longestStrongest := calculateStrongest(bridges, longest) |
| 37 | + fmt.Printf("Part 2 | Longest which is the strongest has %d strength\n", longestStrongest) |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +// Find all possible bridges |
| 42 | +func findBridges(currentPort int, binds []Bind, used []bool, currentBridge []Bind, allBridges *[][]Bind) { |
| 43 | + // Store the current bridge |
| 44 | + bridgeCopy := make([]Bind, len(currentBridge)) |
| 45 | + copy(bridgeCopy, currentBridge) |
| 46 | + *allBridges = append(*allBridges, bridgeCopy) |
| 47 | + |
| 48 | + // Explore all possible connections |
| 49 | + for i, bind := range binds { |
| 50 | + if !used[i] { |
| 51 | + if bind.End1 == currentPort { |
| 52 | + used[i] = true |
| 53 | + findBridges(bind.End2, binds, used, append(currentBridge, bind), allBridges) |
| 54 | + used[i] = false |
| 55 | + } else if bind.End2 == currentPort { |
| 56 | + used[i] = true |
| 57 | + findBridges(bind.End1, binds, used, append(currentBridge, bind), allBridges) |
| 58 | + used[i] = false |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func calculateStrength(bridge []Bind) int { |
| 65 | + strength := 0 |
| 66 | + for _, bind := range bridge { |
| 67 | + strength += bind.End1 + bind.End2 |
| 68 | + } |
| 69 | + return strength |
| 70 | +} |
| 71 | + |
| 72 | +func longestSlice(slices [][]Bind) int { |
| 73 | + longest := 0 |
| 74 | + for _, slice := range slices { |
| 75 | + currentLength := len(slice) |
| 76 | + if currentLength > longest { |
| 77 | + longest = currentLength |
| 78 | + } |
| 79 | + } |
| 80 | + return longest |
| 81 | +} |
| 82 | + |
| 83 | +// Calculate the strongest bridge from the set of the longest ones. |
| 84 | +func calculateStrongest(bridges [][]Bind, length int) int { |
| 85 | + max := 0 |
| 86 | + for _, bridge := range bridges { |
| 87 | + // Its the longest/one of the longest. |
| 88 | + if len(bridge) == length { |
| 89 | + str := calculateStrength(bridge) |
| 90 | + if str > max { |
| 91 | + max = str |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return max |
| 96 | +} |
| 97 | + |
| 98 | +func createBinds() []Bind { |
| 99 | + b := []Bind{} |
| 100 | + |
| 101 | + f, _ := os.Open("input.txt") |
| 102 | + scanner := bufio.NewScanner(f) |
| 103 | + id := 0 |
| 104 | + for scanner.Scan() { |
| 105 | + line := strings.Split(scanner.Text(), "/") |
| 106 | + end0, _ := strconv.Atoi(line[0]) |
| 107 | + end1, _ := strconv.Atoi(line[1]) |
| 108 | + bind := Bind{id, end0, end1} |
| 109 | + id++ |
| 110 | + b = append(b, bind) |
| 111 | + } |
| 112 | + return b |
| 113 | +} |
0 commit comments