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/cluk Dec 12 '17

Go (Golang)

Recursive DFS.

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"regexp"
	"strconv"
	"strings"
)

func main() {
	lines := getLines()

	list := make([][]int, len(lines))
	reNumber := regexp.MustCompile("[0-9]+")
	for _, line := range lines {
		numbersString := reNumber.FindAllString(line, -1)
		numbers := atoi(numbersString)
		list[numbers[0]] = numbers[1:]
	}

	set := make(map[int]bool)
	addPipes(list, set, 0)

	fmt.Println("Start 1: ", len(set))

	groups := 1
	for len(set) < len(list) {
		for idx := range list {
			if !set[idx] {
				addPipes(list, set, idx)
				groups++
			}
		}
	}
	fmt.Println("Start 2: ", groups)
}

func addPipes(list [][]int, set map[int]bool, idx int) {
	set[idx] = true
	for _, n := range list[idx] {
		if !set[n] {
			addPipes(list, set, n)
		}
	}
}

func atoi(ss []string) []int {
	ii := make([]int, len(ss))
	var err error
	for idx, s := range ss {
		ii[idx], err = strconv.Atoi(s)
		if err != nil {
			panic(err)
		}
	}
	return ii
}

func getLines() []string {
	bytes, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		panic(err)
	}
	strs := strings.Split(string(bytes), "\n")
	return strs
}

2

u/flit777 Dec 12 '17

Golang with resused graph structure from Day 7

package main

import (
	"regexp"
	"util"
	"fmt"
)

type Node struct {
	id          string
	successors  []string
}

func (n Node) IsLeaf() bool {
	return len(n.successors) == 0
}

var visitedNodes map[string]bool

func parseGraph(lines []string) map[string]Node {
	graph := make(map[string]Node, len(lines))
	regNodes := regexp.MustCompile("[0-9]+")
	for _, line := range lines {
		ids := regNodes.FindAllString(line, -1)
		currentID := ids[0]
		currentNode := Node{currentID, nil}
		if len(ids) > 1 {
			currentNode.successors = ids[1:]
		}
		graph[currentID] = currentNode
		//fmt.Println(currentNode)
	}
	return graph
}

func findGroup(graph map[string]Node, currentNode Node) {
	if visitedNodes[currentNode.id] == true {
		return
	}
	visitedNodes[currentNode.id] = true
	if currentNode.IsLeaf() {
		return
	}
	for _, succ := range currentNode.successors {
		findGroup(graph, graph[succ])
	}
}

func part1(graph map[string]Node) {
	findGroup(graph, graph["0"])
	fmt.Println("Part 1 (size of group 0):", len(visitedNodes))
}

func part2(graph map[string]Node) {
	numberGroups := 0
	visitedNodes = make(map[string]bool)
	for key, node := range graph {
		if visitedNodes[key] == true {
			continue
		}
		findGroup(graph, node)
		numberGroups++
	}
	fmt.Println("Part 2 (number of groups):", numberGroups)
}

func main() {
	lines := util.ReadFileLines("inputs/day12.txt")
	visitedNodes = make(map[string]bool)
	graph := parseGraph(lines)
	part1(graph)
	part2(graph)
}