Day 12: pipes

This commit is contained in:
Meutel 2017-12-23 17:47:38 +01:00
parent 2e2bad9f57
commit 21f04e6b91
2 changed files with 101 additions and 0 deletions

7
12dec/input_test Normal file
View File

@ -0,0 +1,7 @@
0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5

94
12dec/main.go Normal file
View File

@ -0,0 +1,94 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Prog struct {
Num int
Cx []*Prog
}
var (
Progs map[int]*Prog
)
func main() {
sc := bufio.NewScanner(os.Stdin)
Progs = make(map[int]*Prog)
for sc.Scan() {
fmt.Println(sc.Text())
parse(sc.Text())
}
nums := make([]int, 0, 0)
nums = walk(Progs[0], nums)
fmt.Println("result ", len(nums))
}
func appendUniq(nums []int, num int) ([]int, bool) {
for _, n := range nums {
if n == num {
return nums, false
}
}
nums = append(nums, num)
return nums, true
}
func walk(p *Prog, nums []int) []int {
var added bool
nums, added = appendUniq(nums, p.Num)
if added {
for _, pp := range p.Cx {
nums = walk(pp, nums)
}
}
return nums
}
func parse(in string) {
expr := strings.Split(in, " <-> ")
num, _ := strconv.Atoi(expr[0])
p1 := GetProg(num)
list := strings.Split(expr[1], ", ")
for _, l := range list {
nl, _ := strconv.Atoi(l)
p2 := GetProg(nl)
Pipe(p1, p2)
Pipe(p2, p1)
}
}
func NewProg(num int) *Prog {
return &Prog{
num,
make([]*Prog, 0, 0),
}
}
func GetProg(num int) *Prog {
p := Progs[num]
if p == nil {
p = NewProg(num)
Progs[num] = p
}
return p
}
func Pipe(p1, p2 *Prog) {
piped := false
for _, p := range p1.Cx {
if p1.Num == p.Num {
piped = true
}
}
if !piped {
p1.Cx = append(p1.Cx, p2)
fmt.Println("pipe", p1.Num, p2.Num)
}
}