diff --git a/12dec/input_test b/12dec/input_test new file mode 100644 index 0000000..71c34d4 --- /dev/null +++ b/12dec/input_test @@ -0,0 +1,7 @@ +0 <-> 2 +1 <-> 1 +2 <-> 0, 3, 4 +3 <-> 2, 4 +4 <-> 2, 3, 6 +5 <-> 6 +6 <-> 4, 5 diff --git a/12dec/main.go b/12dec/main.go new file mode 100644 index 0000000..772cc6d --- /dev/null +++ b/12dec/main.go @@ -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) + } +}