2017 day 12 done!

This commit is contained in:
Brian Buller 2017-12-12 07:48:53 -06:00
parent 2f65fcff50
commit a43d9c4ca7
4 changed files with 2217 additions and 0 deletions

128
2017/day12/day12.go Normal file
View File

@ -0,0 +1,128 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
var progs map[int][]int
var groups map[int][]int
func main() {
inp := StdinToStrings()
part1(inp)
part2()
}
func part1(inp []string) {
progs = make(map[int][]int)
for i := range inp {
pts := strings.Split(inp[i], " ")
mn := Atoi(pts[0])
for _, v := range pts[2:] {
if v[len(v)-1] == ',' {
v = v[:len(v)-1]
}
if v = strings.TrimSpace(v); v != "" {
sub := Atoi(v)
progs[mn] = append(progs[mn], sub)
}
}
}
var res int
for k := range progs {
if pRes, _ := progCanAccess(k, 0, []int{}); pRes {
res++
}
}
fmt.Println(res, "programs can access zero")
}
func part2() {
groups = make(map[int][]int)
for k, v := range progs {
isNew := true
for gk := range groups {
if can, _ := progCanAccess(k, gk, []int{}); can {
AddToGroup(gk, k)
isNew = false
break
}
}
if isNew {
// We went through all groups and couldn't access any
groups[k] = v
}
}
fmt.Println(len(groups), "total groups")
}
func AddToGroup(grp, prog int) {
if !SliceContains(groups[grp], prog) {
groups[grp] = append(groups[grp], prog)
}
for _, v := range progs[prog] {
if !SliceContains(groups[grp], v) {
groups[grp] = append(groups[grp], v)
}
}
}
func progCanAccess(st, end int, tried []int) (bool, []int) {
// Have we already tried this one?
if SliceContains(tried, st) {
return false, tried
}
tried = append(tried, st)
// Is this it?
if st == end {
return true, tried
}
// Is it direct?
if SliceContains(progs[st], end) {
return true, tried
}
// Ok, recurse
var res bool
for _, v := range progs[st] {
if res, tried = progCanAccess(v, end, tried); res {
return res, tried
}
}
return false, tried
}
func SliceContains(sl []int, tgt int) bool {
for i := range sl {
if sl[i] == tgt {
return true
}
}
return false
}
func StdinToStrings() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}

2000
2017/day12/input Normal file

File diff suppressed because it is too large Load Diff

82
2017/day12/problem Normal file
View File

@ -0,0 +1,82 @@
Advent of Code
--- Day 12: Digital Plumber ---
Walking along the memory banks of the stream, you find a small village that is experiencing a little confusion:
some programs can't communicate with each other.
Programs in this village communicate using a fixed system of pipes. Messages are passed between programs using
these pipes, but most programs aren't connected to each other directly. Instead, programs pass messages between
each other until the message reaches the intended recipient.
For some reason, though, some of these messages aren't ever reaching their intended recipient, and the programs
suspect that some pipes are missing. They would like you to investigate.
You walk through the village and record the ID of each program and the IDs with which it can communicate directly
(your puzzle input). Each program has one or more programs with which it can communicate, and these pipes are
bidirectional; if 8 says it can communicate with 11, then 11 will say it can communicate with 8.
You need to figure out how many programs are in the group that contains program ID 0.
For example, suppose you go door-to-door like a travelling salesman and record the following list:
0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5
In this example, the following programs are in the group that contains program ID 0:
 Program 0 by definition.
 Program 2, directly connected to program 0.
 Program 3 via program 2.
 Program 4 via program 2.
 Program 5 via programs 6, then 4, then 2.
 Program 6 via programs 4, then 2.
Therefore, a total of 6 programs are in this group; all but program 1, which has a pipe that connects it to
itself.
How many programs are in the group that contains program ID 0?
Your puzzle answer was _____.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
There are more programs than just the ones in the group containing program ID 0. The rest of them have no way of
reaching that group, and still might have no way of reaching each other.
A group is a collection of programs that can all communicate via pipes either directly or indirectly. The
programs you identified just a moment ago are all part of the same group. Now, they would like you to determine
the total number of groups.
In the example above, there were 2 groups: one consisting of programs 0,2,3,4,5,6, and the other consisting
solely of program 1.
How many groups are there in total?
Although it hasn't changed, you can still get your puzzle input.
Answer: _____________________
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2017/about
. http://adventofcode.com/2017/support
. http://adventofcode.com/2017/events
. http://adventofcode.com/2017/settings
. http://adventofcode.com/2017/auth/logout
. http://adventofcode.com/2017
. http://adventofcode.com/2017
. http://adventofcode.com/2017/leaderboard
. http://adventofcode.com/2017/stats
. http://adventofcode.com/2017/sponsors
. http://adventofcode.com/2017/sponsors
. http://adventofcode.com/2017/day/12/input

7
2017/day12/testinput 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