-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (57 loc) · 1.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package day07 - Solution for Advent of Code 2018/07
// Problem Link: http://adventofcode.com/2018/day/07
package day07
import (
_ "embed"
"fmt"
"github.com/code-shoily/aocgo/algo/graphs"
"github.com/code-shoily/aocgo/io"
"strings"
)
//go:embed input.txt
var input string
// Run prints out the result of the solution.
func Run() {
fmt.Println(solve(input))
}
func solve(input string) (string, int) {
data := parse(input)
return solvePart1(data), solvePart2(data)
}
func solvePart1(g *graphs.Graph) string {
if sorted, cycle := graphs.LexicographicalTopologicalSort(g); !cycle {
return strings.Join(sorted, "")
}
panic("Cycle detected :O")
}
func solvePart2(g *graphs.Graph) int {
return 0
}
func parse(input string) *graphs.Graph {
var dependencyPairs [][2]string
for _, line := range io.SplitLines(input) {
var from, to string
fmt.Sscanf(line, "Step %s must be finished before step %s can begin.", &from, &to)
dependencyPairs = append(dependencyPairs, [2]string{from, to})
}
return MakeGraph(dependencyPairs)
}
func MakeGraph(deps [][2]string) *graphs.Graph {
graph := graphs.NewGraph(true)
vertices := map[string]*graphs.Vertex{}
for _, dep := range deps {
from := getVertex(dep[0], vertices)
to := getVertex(dep[1], vertices)
graph.AddVertex(from)
graph.AddVertex(to)
graph.AddEdge(from.ID(), to.ID(), 1)
}
return graph
}
func getVertex(key string, vertexMap map[string]*graphs.Vertex) *graphs.Vertex {
if vertex, exists := vertexMap[key]; exists {
return vertex
}
vertexMap[key] = graphs.NewSimpleVertex(key)
return vertexMap[key]
}