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

Appearance settings

Commit b853f51

Browse filesBrowse files
committed
Solution for 2023, day 25
1 parent 79bcaf1 commit b853f51
Copy full SHA for b853f51

File tree

Expand file treeCollapse file tree

3 files changed

+98
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+98
-0
lines changed

‎2023/25/Makefile

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
main1:
2+
go build -o main1 main1.go
3+
4+
.PHONY: run1 clean
5+
6+
run1: main1
7+
./main1 <input
8+
9+
clean:
10+
rm -f main1
11+

‎2023/25/graph.png

Copy file name to clipboard
1.33 MB
Loading

‎2023/25/main1.go

Copy file name to clipboard
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
type Graph map[string]map[string]struct{}
11+
12+
func (g Graph) Link(a, b string) {
13+
if _, ok := g[a]; !ok {
14+
g[a] = make(map[string]struct{})
15+
}
16+
g[a][b] = struct{}{}
17+
18+
if _, ok := g[b]; !ok {
19+
g[b] = make(map[string]struct{})
20+
}
21+
g[b][a] = struct{}{}
22+
}
23+
24+
func (g Graph) Unlink(a, b string) {
25+
delete(g[a], b)
26+
delete(g[b], a)
27+
}
28+
29+
func (g Graph) Dot() string {
30+
sb := strings.Builder{}
31+
sb.WriteString("graph G {\n")
32+
for a := range g {
33+
for b := range g[a] {
34+
sb.WriteString(fmt.Sprintf("\t%s -- %s\n", a, b))
35+
}
36+
}
37+
sb.WriteString("}\n")
38+
return sb.String()
39+
}
40+
41+
func parse() Graph {
42+
g := Graph{}
43+
scanner := bufio.NewScanner(os.Stdin)
44+
for scanner.Scan() {
45+
line := scanner.Text()
46+
47+
ff := strings.Split(line, ":")
48+
left := ff[0]
49+
right := strings.Fields(ff[1])
50+
51+
for _, r := range right {
52+
g.Link(left, r)
53+
}
54+
}
55+
return g
56+
}
57+
58+
func main() {
59+
g := parse()
60+
61+
// Uncomment the following line to print the .dot file.
62+
//fmt.Println(g.Dot())
63+
// Use `neato -T png graph.dot >graph.png`
64+
// to generate the image included in the dir.
65+
66+
g.Unlink("vps", "pzc")
67+
g.Unlink("xvk", "sgc")
68+
g.Unlink("dph", "cvx")
69+
70+
fmt.Println(bfs(g, "vps") * bfs(g, "pzc"))
71+
}
72+
73+
func bfs(g Graph, start string) int {
74+
visited := map[string]struct{}{}
75+
toDo := []string{start}
76+
var curr string
77+
for len(toDo) > 0 {
78+
curr, toDo = toDo[0], toDo[1:]
79+
for node := range g[curr] {
80+
if _, ok := visited[node]; !ok {
81+
visited[node] = struct{}{}
82+
toDo = append(toDo, node)
83+
}
84+
}
85+
}
86+
return len(visited)
87+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.