-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
155 lines (131 loc) · 2.57 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bufio"
lib "github.com/teivah/advent-of-code"
"io"
"strconv"
"strings"
)
func fs1(input io.Reader) (string, error) {
scanner := bufio.NewScanner(input)
towers := make(map[string]Tower)
for scanner.Scan() {
s := scanner.Text()
tower := toTower(s)
towers[tower.name] = tower
}
return getBottom(towers), nil
}
func getBottom(towers map[string]Tower) string {
inDegree := make(map[string]int)
for name := range towers {
inDegree[name] = 0
}
for _, tower := range towers {
for _, child := range tower.children {
inDegree[child]++
}
}
for k, v := range inDegree {
if v == 0 {
return k
}
}
return ""
}
type Tower struct {
name string
weight int
children []string
}
func toTower(s string) Tower {
spaces := lib.IndexAll(s, " ")
name := s[:spaces[0]]
v := s[strings.Index(s, "(")+1 : strings.Index(s, ")")]
weight, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
var children []string
if sep := strings.Index(s, "->"); sep != -1 {
s = s[sep+3:]
children = strings.Split(s, ", ")
}
return Tower{
name: name,
weight: weight,
children: children,
}
}
func fs2(input io.Reader) (int, error) {
scanner := bufio.NewScanner(input)
towers := make(map[string]Tower)
for scanner.Scan() {
s := scanner.Text()
tower := toTower(s)
towers[tower.name] = tower
}
_, v := fix(towers, getBottom(towers))
return v, nil
}
// Recursive weight, fix
func fix(towers map[string]Tower, current string) (int, int) {
parent := towers[current]
if len(parent.children) == 0 {
return parent.weight, -1
}
v, res := fix(towers, parent.children[0])
if res != -1 {
return 0, res
}
sum := v
expected := v
got := 0
found := false
weigths := make(map[string]int)
weigths[parent.children[0]] = v
for i := 1; i < len(parent.children); i++ {
got, v = fix(towers, parent.children[i])
if v != -1 {
return 0, v
}
if got != expected {
found = true
}
sum += got
weigths[parent.children[i]] = got
}
if !found {
return sum + parent.weight, -1
}
wrongChildren, wrongWeight := findWrongWeight(weigths)
good := 0
if wrongWeight == expected {
good = got
} else {
good = expected
}
sum += parent.weight
total := parent.weight + 3*good
diff := total - sum
return 0, towers[wrongChildren].weight + diff
}
func findWrongWeight(weights map[string]int) (string, int) {
res := make(map[int]int)
for _, v := range weights {
res[v]++
}
weight := 0
for k, v := range res {
if v == 1 {
weight = k
break
}
}
for k, v := range weights {
if v == weight {
return k, v
}
}
return "", 0
}