go fmt
This commit is contained in:
parent
0eec315f95
commit
bf5ef31334
2 changed files with 14 additions and 16 deletions
15
12/12.go
15
12/12.go
|
@ -4,8 +4,8 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cat input | go run 12.go
|
// cat input | go run 12.go
|
||||||
|
@ -24,7 +24,7 @@ func main() {
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
|
|
||||||
s := strings.Split(scanner.Text(), " <-> ")
|
s := strings.Split(scanner.Text(), " <-> ")
|
||||||
pipe,_ := strconv.Atoi(s[0])
|
pipe, _ := strconv.Atoi(s[0])
|
||||||
|
|
||||||
var linkedTo = []int{}
|
var linkedTo = []int{}
|
||||||
l := strings.Split(s[1], ", ")
|
l := strings.Split(s[1], ", ")
|
||||||
|
@ -38,12 +38,11 @@ func main() {
|
||||||
|
|
||||||
cp := CountPipes(pd)
|
cp := CountPipes(pd)
|
||||||
seenGroups := make(map[int]int)
|
seenGroups := make(map[int]int)
|
||||||
groups,_ := CountGroups(seenGroups, pd)
|
groups, _ := CountGroups(seenGroups, pd)
|
||||||
|
|
||||||
fmt.Println(cp,groups)
|
fmt.Println(cp, groups)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func CountPipes(pipedata []pipedata) int {
|
func CountPipes(pipedata []pipedata) int {
|
||||||
seen := make(map[int]int)
|
seen := make(map[int]int)
|
||||||
seen = getChildren(seen, pipedata, 0)
|
seen = getChildren(seen, pipedata, 0)
|
||||||
|
@ -58,7 +57,7 @@ func CountPipes(pipedata []pipedata) int {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CountGroups(seen map[int]int, pipedata []pipedata) (int,map[int]int) {
|
func CountGroups(seen map[int]int, pipedata []pipedata) (int, map[int]int) {
|
||||||
count := 0
|
count := 0
|
||||||
for i := range pipedata {
|
for i := range pipedata {
|
||||||
if seen[i] > 0 {
|
if seen[i] > 0 {
|
||||||
|
@ -68,7 +67,7 @@ func CountGroups(seen map[int]int, pipedata []pipedata) (int,map[int]int) {
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return count,seen
|
return count, seen
|
||||||
}
|
}
|
||||||
|
|
||||||
func getChildren(seen map[int]int, pipedata []pipedata, pipe int) map[int]int {
|
func getChildren(seen map[int]int, pipedata []pipedata, pipe int) map[int]int {
|
||||||
|
@ -76,7 +75,7 @@ func getChildren(seen map[int]int, pipedata []pipedata, pipe int) map[int]int {
|
||||||
p := pipedata[pipe]
|
p := pipedata[pipe]
|
||||||
for l := range p.linkedTo {
|
for l := range p.linkedTo {
|
||||||
child := p.linkedTo[l]
|
child := p.linkedTo[l]
|
||||||
if (seen[child] == 0) {
|
if seen[child] == 0 {
|
||||||
seen[child]++
|
seen[child]++
|
||||||
seen = getChildren(seen, pipedata, child)
|
seen = getChildren(seen, pipedata, child)
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,9 @@ func TestCountPipes(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestCountGroups(t *testing.T) {
|
func TestCountGroups(t *testing.T) {
|
||||||
seen := make(map[int]int)
|
seen := make(map[int]int)
|
||||||
v,_ := CountGroups(seen, tests)
|
v, _ := CountGroups(seen, tests)
|
||||||
if v != 2 {
|
if v != 2 {
|
||||||
t.Error(
|
t.Error(
|
||||||
"For", tests,
|
"For", tests,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue