-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
input: | ||
http "https://adventofcode.com/2024/day/23/input" "Cookie:session=${AOC_SESSION};" >input | ||
|
||
main1: | ||
go build -o main1 main1.go common.go | ||
|
||
main2: | ||
go build -o main2 main2.go common.go | ||
|
||
.PHONY: run1 run2 clean | ||
|
||
run1: main1 input | ||
./main1 <input | ||
|
||
run2: main2 input | ||
./main2 <input | ||
|
||
clean: | ||
rm -f main1 main2 input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type Set map[string]struct{} | ||
|
||
func (s *Set) ToKey(others ...string) string { | ||
list := make([]string, 0, len(*s)+len(others)) | ||
for k := range *s { | ||
list = append(list, k) | ||
} | ||
for _, other := range others { | ||
list = append(list, other) | ||
} | ||
sort.Strings(list) | ||
return strings.Join(list, ",") | ||
} | ||
|
||
func (s *Set) FromKey(key string) { | ||
m := Set{} | ||
for _, s := range strings.Split(key, ",") { | ||
m[s] = struct{}{} | ||
} | ||
*s = m | ||
} | ||
|
||
type Graph map[string]Set | ||
|
||
func (g Graph) AddEdge(from, to string) { | ||
if _, ok := g[from]; !ok { | ||
g[from] = Set{} | ||
} | ||
g[from][to] = struct{}{} | ||
} | ||
|
||
func (g Graph) Connected(from, to string) bool { | ||
if f, ok := g[from]; ok { | ||
_, ok := f[to] | ||
return ok | ||
} | ||
return false | ||
} | ||
|
||
func (g Graph) Keys() Set { | ||
keys := Set{} | ||
for n := range g { | ||
keys[n] = struct{}{} | ||
} | ||
return keys | ||
} | ||
|
||
func parseInput() Graph { | ||
graph := Graph{} | ||
scanner := bufio.NewScanner(os.Stdin) | ||
for scanner.Scan() { | ||
ff := strings.Split(scanner.Text(), "-") | ||
graph.AddEdge(ff[0], ff[1]) | ||
graph.AddEdge(ff[1], ff[0]) | ||
} | ||
return graph | ||
} | ||
|
||
func (g Graph) Expand(clusters Set) Set { | ||
res := Set{} | ||
for clusterStr := range clusters { | ||
cluster := Set{} | ||
cluster.FromKey(clusterStr) | ||
for node := range g[clusterStr[:2]] { | ||
if _, ok := cluster[node]; ok { | ||
continue | ||
} | ||
if !g.connectedToAll(node, cluster) { | ||
continue | ||
} | ||
res[cluster.ToKey(node)] = struct{}{} | ||
} | ||
} | ||
return res | ||
} | ||
|
||
func (g Graph) connectedToAll(node string, cluster Set) bool { | ||
for old := range cluster { | ||
if !g.Connected(old, node) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
g := parseInput() | ||
|
||
res := g.Keys() // single nodes | ||
res = g.Expand(res) // pairs | ||
res = g.Expand(res) // triplets | ||
|
||
count := 0 | ||
for c := range res { | ||
if c[0] == 't' || strings.Contains(c, ",t") { | ||
count++ | ||
} | ||
} | ||
fmt.Println(count) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
g := parseInput() | ||
|
||
var lastNotEmpty Set | ||
for res := g.Keys(); len(res) > 0; res = g.Expand(res) { | ||
lastNotEmpty = res | ||
} | ||
|
||
for k := range lastNotEmpty { | ||
fmt.Println(k) | ||
return | ||
} | ||
} |