Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fcd1fe3

Browse files
committed
Day 24 done, with a little special help from my friend. Get with the
times.
1 parent b544a53 commit fcd1fe3

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

‎2017/day24/input.txt‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
25/13
2+
4/43
3+
42/42
4+
39/40
5+
17/18
6+
30/7
7+
12/12
8+
32/28
9+
9/28
10+
1/1
11+
16/7
12+
47/43
13+
34/16
14+
39/36
15+
6/4
16+
3/2
17+
10/49
18+
46/50
19+
18/25
20+
2/23
21+
3/21
22+
5/24
23+
46/26
24+
50/19
25+
26/41
26+
1/50
27+
47/41
28+
39/50
29+
12/14
30+
11/19
31+
28/2
32+
38/47
33+
5/5
34+
38/34
35+
39/39
36+
17/34
37+
42/16
38+
32/23
39+
13/21
40+
28/6
41+
6/20
42+
1/30
43+
44/21
44+
11/28
45+
14/17
46+
33/33
47+
17/43
48+
31/13
49+
11/21
50+
31/39
51+
0/9
52+
13/50
53+
10/14
54+
16/10
55+
3/24
56+
7/0
57+
50/50

‎2017/day24/twenty-four.go‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /