Skip to content

Commit 3aa020a

Browse files
committed
2024 day 23
1 parent de5cee3 commit 3aa020a

File tree

5 files changed

+3647
-0
lines changed

5 files changed

+3647
-0
lines changed

2024/day23/day23.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}
19+
20+
var computers map[string]Computer
21+
22+
func main() {
23+
filenames := []string{"testInput", "input"}
24+
for _, fileName := range filenames {
25+
fileContents, err := os.ReadFile(fileName)
26+
if err != nil {
27+
panic(err)
28+
}
29+
fmt.Println(fileName)
30+
total := 0
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+
threeSets := make([][]string, 0)
58+
for _, computer := range computers {
59+
for _, connectedComputerName := range computer.connectedComputers {
60+
for _, connectedToConnectedName := range computers[connectedComputerName].connectedComputers {
61+
if slices.Contains(computer.connectedComputers, connectedToConnectedName) {
62+
alreadyExists := false
63+
for _, set := range threeSets {
64+
if set[0] == computer.name && set[1] == connectedComputerName && set[2] == connectedToConnectedName {
65+
alreadyExists = true
66+
break
67+
}
68+
if set[0] == computer.name && set[1] == connectedToConnectedName && set[2] == connectedComputerName {
69+
alreadyExists = true
70+
break
71+
}
72+
if set[0] == connectedComputerName && set[1] == computer.name && set[2] == connectedToConnectedName {
73+
alreadyExists = true
74+
break
75+
}
76+
if set[0] == connectedComputerName && set[1] == connectedToConnectedName && set[2] == computer.name {
77+
alreadyExists = true
78+
break
79+
}
80+
if set[0] == connectedToConnectedName && set[1] == connectedComputerName && set[2] == computer.name {
81+
alreadyExists = true
82+
break
83+
}
84+
if set[0] == connectedToConnectedName && set[1] == computer.name && set[2] == connectedComputerName {
85+
alreadyExists = true
86+
break
87+
}
88+
}
89+
if !alreadyExists {
90+
newSet := []string{computer.name, connectedComputerName, connectedToConnectedName}
91+
sort.Strings(newSet)
92+
threeSets = append(threeSets, newSet)
93+
}
94+
}
95+
}
96+
}
97+
}
98+
for _, set := range threeSets {
99+
for _, name := range set {
100+
if string(name[0]) == "t" {
101+
//println(strings.Join(set, ","))
102+
total++
103+
break
104+
}
105+
}
106+
}
107+
108+
println(total)
109+
}
110+
}

2024/day23/day23_pt2.go

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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy