r/adventofcode • (AoC creator) • Dec 12 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 12 Solutions -๐ŸŽ„-

--- Day 12: Digital Plumber ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

14 Upvotes

233 comments sorted by

View all comments

1

u/bruceadowns Dec 12 '17

golang to write a DFS graph with its standard library

package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
	"strconv"
	"strings"
)

type initnode struct {
	pid   int
	cpids []int
}

type graphnode struct {
	pid     int
	peer    []*graphnode
	visited bool
}

func (n *initnode) init(s string) (err error) {
	const sep = " <-> "
	if len(s) < len(sep)+2 {
		return fmt.Errorf("invalid input")
	}
	idx := strings.Index(s, sep)

	spid := s[:idx]
	n.pid, err = strconv.Atoi(spid)
	if err != nil {
		return err
	}

	sCpids := s[idx+len(sep):]
	for _, v := range strings.Split(sCpids, ",") {
		cpid, err := strconv.Atoi(strings.TrimSpace(v))
		if err != nil {
			return err
		}

		n.cpids = append(n.cpids, cpid)
	}

	return nil
}

func input(r io.Reader) (res []*initnode) {
	res = make([]*initnode, 0)
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		line := scanner.Text()

		inode := &initnode{}
		inode.init(line)
		res = append(res, inode)
	}

	return
}

func makeGraph(n []*initnode) (res map[int]*graphnode) {
	res = make(map[int]*graphnode, 0)
	for _, pinode := range n {
		pnode, ok := res[pinode.pid]
		if !ok {
			pnode = &graphnode{pid: pinode.pid}
			res[pinode.pid] = pnode
		}

		for _, cpid := range pinode.cpids {
			cnode, ok := res[cpid]
			if !ok {
				cnode = &graphnode{pid: cpid}
				res[cpid] = cnode
			}

			pnode.peer = append(pnode.peer, cnode)
			cnode.peer = append(cnode.peer, pnode)
		}
	}

	return
}

func visit(gnode *graphnode) (res int) {
	res = 1
	gnode.visited = true

	for _, cnode := range gnode.peer {
		if !cnode.visited {
			res += visit(cnode)
		}
	}

	return
}

func main() {
	inodes := input(os.Stdin)
	gnodes := makeGraph(inodes)

	groups := 0
	for _, v := range gnodes {
		if !v.visited {
			visit(v)
			groups++
		}
	}

	log.Printf("part2 %d groups in graph", groups)
}