diff --git a/12/12.go b/12/12.go index f6c93c5..8cf3d06 100644 --- a/12/12.go +++ b/12/12.go @@ -4,14 +4,14 @@ import ( "bufio" "fmt" "os" - "strings" "strconv" + "strings" ) // cat input | go run 12.go type pipedata struct { - pipe int + pipe int linkedTo []int } @@ -24,7 +24,7 @@ func main() { for scanner.Scan() { s := strings.Split(scanner.Text(), " <-> ") - pipe,_ := strconv.Atoi(s[0]) + pipe, _ := strconv.Atoi(s[0]) var linkedTo = []int{} l := strings.Split(s[1], ", ") @@ -38,17 +38,16 @@ func main() { cp := CountPipes(pd) 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 { seen := make(map[int]int) seen = getChildren(seen, pipedata, 0) fmt.Println(seen) - + count := 0 for range seen { count++ @@ -58,17 +57,17 @@ 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 for i := range pipedata { if seen[i] > 0 { continue } else { seen = getChildren(seen, pipedata, i) - count++ + count++ } } - return count,seen + return count, seen } func getChildren(seen map[int]int, pipedata []pipedata, pipe int) map[int]int { @@ -76,11 +75,11 @@ func getChildren(seen map[int]int, pipedata []pipedata, pipe int) map[int]int { p := pipedata[pipe] for l := range p.linkedTo { child := p.linkedTo[l] - if (seen[child] == 0) { + if seen[child] == 0 { seen[child]++ seen = getChildren(seen, pipedata, child) - } + } } return seen -} \ No newline at end of file +} diff --git a/12/12_test.go b/12/12_test.go index 33c6e18..8bf2800 100644 --- a/12/12_test.go +++ b/12/12_test.go @@ -25,10 +25,9 @@ func TestCountPipes(t *testing.T) { } } - func TestCountGroups(t *testing.T) { seen := make(map[int]int) - v,_ := CountGroups(seen, tests) + v, _ := CountGroups(seen, tests) if v != 2 { t.Error( "For", tests, @@ -36,4 +35,4 @@ func TestCountGroups(t *testing.T) { "got", v, ) } -} \ No newline at end of file +}