Skip to content

Commit f2fdd86

Browse files
committed
Solve 18/1 - part 1 :(
1 parent a7079a9 commit f2fdd86

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

year18/day07/main.go

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

year18/day07/main_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package day07
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSolve(t *testing.T) {
8+
solve1, solve2 := solve(input)
9+
expected1, expected2 := 0, 0
10+
11+
if solve1 != expected1 {
12+
t.Errorf("Fail - part 1. Expected %d, got %d", expected1, solve1)
13+
}
14+
15+
if solve2 != expected2 {
16+
t.Errorf("Fail - part 2. Expected %d, got %d", expected2, solve2)
17+
}
18+
}
19+
20+
func BenchmarkSolvePart1(b *testing.B) {
21+
data := parse(input)
22+
for i := 0; i < b.N; i++ {
23+
solvePart1(data)
24+
}
25+
}
26+
27+
func BenchmarkSolvePart2(b *testing.B) {
28+
data := parse(input)
29+
for i := 0; i < b.N; i++ {
30+
solvePart2(data)
31+
}
32+
}
33+
34+
func BenchmarkSolve(b *testing.B) {
35+
for i := 0; i < b.N; i++ {
36+
solve(input)
37+
}
38+
}

year18/runner.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/code-shoily/aocgo/year18/day03"
88
"github.com/code-shoily/aocgo/year18/day04"
99
"github.com/code-shoily/aocgo/year18/day05"
10+
"github.com/code-shoily/aocgo/year18/day07"
1011
)
1112

1213
func SolveForDay(day int) {
@@ -21,6 +22,8 @@ func SolveForDay(day int) {
2122
day04.Run()
2223
case 5:
2324
day05.Run()
25+
case 7:
26+
day07.Run()
2427
default:
2528
fmt.Printf("2018/%d has not been solved yet", day)
2629
}

0 commit comments

Comments
 (0)