day 12
This commit is contained in:
parent
d5909ca148
commit
d94663d310
2 changed files with 126 additions and 0 deletions
86
12/12.go
Normal file
86
12/12.go
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// cat input | go run 12.go
|
||||||
|
|
||||||
|
type pipedata struct {
|
||||||
|
pipe int
|
||||||
|
linkedTo []int
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
|
||||||
|
var pd = []pipedata{}
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
|
||||||
|
s := strings.Split(scanner.Text(), " <-> ")
|
||||||
|
pipe,_ := strconv.Atoi(s[0])
|
||||||
|
|
||||||
|
var linkedTo = []int{}
|
||||||
|
l := strings.Split(s[1], ", ")
|
||||||
|
for t := range l {
|
||||||
|
i, _ := strconv.Atoi(l[t])
|
||||||
|
linkedTo = append(linkedTo, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
pd = append(pd, pipedata{pipe, linkedTo})
|
||||||
|
}
|
||||||
|
|
||||||
|
cp := CountPipes(pd)
|
||||||
|
seenGroups := make(map[int]int)
|
||||||
|
groups,_ := CountGroups(seenGroups, pd)
|
||||||
|
|
||||||
|
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++
|
||||||
|
}
|
||||||
|
|
||||||
|
return count
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count,seen
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
seen[child]++
|
||||||
|
seen = getChildren(seen, pipedata, child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return seen
|
||||||
|
}
|
40
12/12_test.go
Normal file
40
12/12_test.go
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
var tests = []pipedata{
|
||||||
|
{0, []int{2}},
|
||||||
|
{1, []int{1}},
|
||||||
|
{2, []int{0, 3, 4}},
|
||||||
|
{3, []int{2, 4}},
|
||||||
|
{4, []int{2, 3, 6}},
|
||||||
|
{5, []int{6}},
|
||||||
|
{6, []int{4, 5}},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCountPipes(t *testing.T) {
|
||||||
|
v := CountPipes(tests)
|
||||||
|
if v != 6 {
|
||||||
|
t.Error(
|
||||||
|
"For", tests,
|
||||||
|
"expected 6",
|
||||||
|
"got", v,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func TestCountGroups(t *testing.T) {
|
||||||
|
seen := make(map[int]int)
|
||||||
|
v,_ := CountGroups(seen, tests)
|
||||||
|
if v != 2 {
|
||||||
|
t.Error(
|
||||||
|
"For", tests,
|
||||||
|
"expected 2",
|
||||||
|
"got", v,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue