Switching computers

This commit is contained in:
Brian Buller 2017-12-26 08:16:20 -06:00
parent 6c4e6ec824
commit e78eb5781e
3 changed files with 268 additions and 0 deletions

203
2017/day24/day24.go Normal file
View File

@ -0,0 +1,203 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
var comps []Component
// Greater than 907
func main() {
inp := StdinToStrings()
for i := range inp {
comps = append(comps, NewComponent(inp[i]))
}
// Need to build chains of all permutations
var perms []Bridge
for i := range comps {
if comps[i].Has(0) {
perms = append(perms, Bridge([]Component{comps[i]}))
}
}
changed := true
for changed {
perms, changed = GetNextPerms(perms)
}
var highB Bridge
highest := 0
for _, b := range perms {
if b.Value() > highest {
highB = b
highest = b.Value()
}
}
fmt.Println(highB)
/*
for _, b := range perms {
tst := GetAllThatFit(b)
for i := range tst {
}
for tst != nil {
b = b.Add(*tst)
tst = GetNextBest(b)
}
fmt.Println(b)
}
*/
}
func GetNextPerms(perms []Bridge) ([]Bridge, bool) {
var ret []Bridge
var changed bool
for i := range perms {
if !perms[i].IsDone() {
allComps := GetAllThatFit(perms[i])
if len(allComps) == 0 {
ret = append(ret, perms[i].Add(NewComponent("0/0")))
}
for j := range allComps {
ret = append(ret, perms[i].Add(allComps[j]))
changed = true
}
}
}
return ret, changed
}
func GetAllThatFit(b Bridge) []Component {
var ret []Component
port := b.Needs()
for i := range comps {
if comps[i].Has(port) && !b.Has(comps[i]) {
ret = append(ret, comps[i])
}
}
return ret
}
func GetNextBest(b Bridge) *Component {
var best *Component
port := b.Needs()
for i := range comps {
if comps[i].Has(port) && !b.Has(comps[i]) {
if best == nil || comps[i].Value() > best.Value() {
best = &comps[i]
}
}
}
return best
}
type Bridge []Component
func (b Bridge) IsDone() bool {
t := []Component(b)
c := t[len(t)-1]
return c.Value() == 0
}
func (b Bridge) Value() int {
var ret int
for _, v := range []Component(b) {
ret += v.Value()
}
return ret
}
func (b Bridge) Has(c Component) bool {
for _, v := range []Component(b) {
if v.Name == c.Name {
return true
}
}
return false
}
func (b Bridge) Add(c Component) Bridge {
b = Bridge(append([]Component(b), c))
return b
}
func (b Bridge) Needs() int {
t := []Component(b)
c := t[len(t)-1]
if len(t) == 1 {
return c.Other(0)
}
c1 := t[len(t)-2]
// Find the side of c that doesn't connect with c1
if c.Has(c1.Side1) {
return c.Other(c1.Side1)
}
return c.Other(c1.Side2)
}
func (b Bridge) String() string {
ret := "[ "
for _, v := range []Component(b) {
ret += v.Name + " "
}
return fmt.Sprint(ret+"] ", b.Value())
}
type Component struct {
Name string
Side1, Side2 int
}
func NewComponent(in string) Component {
c := Component{}
c.Name = in
pts := strings.Split(in, "/")
c.Side1 = Atoi(pts[0])
c.Side2 = Atoi(pts[1])
return c
}
func (c *Component) Equals(t Component) bool {
return c.Side1 == t.Side1 && c.Side2 == t.Side2
}
func (c *Component) Value() int {
return c.Side1 + c.Side2
}
func (c *Component) Has(v int) bool {
return c.Side1 == v || c.Side2 == v
}
func (c *Component) Other(v int) int {
if c.Side1 == v {
return c.Side2
}
return c.Side1
}
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
}
func StdinToStrings() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}

57
2017/day24/input Normal file
View File

@ -0,0 +1,57 @@
25/13
4/43
42/42
39/40
17/18
30/7
12/12
32/28
9/28
1/1
16/7
47/43
34/16
39/36
6/4
3/2
10/49
46/50
18/25
2/23
3/21
5/24
46/26
50/19
26/41
1/50
47/41
39/50
12/14
11/19
28/2
38/47
5/5
38/34
39/39
17/34
42/16
32/23
13/21
28/6
6/20
1/30
44/21
11/28
14/17
33/33
17/43
31/13
11/21
31/39
0/9
13/50
10/14
16/10
3/24
7/0
50/50

8
2017/day24/testinput Normal file
View File

@ -0,0 +1,8 @@
0/2
2/2
2/3
3/4
3/5
0/1
10/1
9/10