2017 Complete!
This commit is contained in:
@@ -10,106 +10,56 @@ import (
|
||||
)
|
||||
|
||||
var comps []Component
|
||||
var strongest, best Bridge
|
||||
|
||||
// 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]}))
|
||||
}
|
||||
}
|
||||
FindStrongest([]Component{NewComponent("0/0")})
|
||||
fmt.Println("Strongest:\n", strongest)
|
||||
|
||||
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)
|
||||
}
|
||||
*/
|
||||
FindBest([]Component{NewComponent("0/0")})
|
||||
fmt.Println("Best:\n", best)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
// Strongest, regardless of length
|
||||
func FindStrongest(bridge Bridge) {
|
||||
if bridge.Strength() > strongest.Strength() {
|
||||
strongest = bridge
|
||||
}
|
||||
for i := range comps {
|
||||
if comps[i].Has(bridge.Needs()) && !bridge.Has(comps[i]) {
|
||||
FindStrongest(append(bridge, comps[i]))
|
||||
}
|
||||
}
|
||||
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])
|
||||
// Longest and Strongest
|
||||
func FindBest(bridge Bridge) {
|
||||
if bridge.Length() > best.Length() {
|
||||
best = append([]Component{}, bridge...)
|
||||
} else if bridge.Length() == best.Length() {
|
||||
if bridge.Strength() > best.Strength() {
|
||||
best = bridge
|
||||
}
|
||||
}
|
||||
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]
|
||||
}
|
||||
if comps[i].Has(bridge.Needs()) && !bridge.Has(comps[i]) {
|
||||
FindBest(append(bridge, 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 {
|
||||
func (b Bridge) Strength() int {
|
||||
var ret int
|
||||
for _, v := range []Component(b) {
|
||||
ret += v.Value()
|
||||
ret += v.Strength()
|
||||
}
|
||||
return ret
|
||||
}
|
||||
@@ -143,12 +93,16 @@ func (b Bridge) Needs() int {
|
||||
return c.Other(c1.Side2)
|
||||
}
|
||||
|
||||
func (b Bridge) Length() int {
|
||||
return len([]Component(b))
|
||||
}
|
||||
|
||||
func (b Bridge) String() string {
|
||||
ret := "[ "
|
||||
for _, v := range []Component(b) {
|
||||
ret += v.Name + " "
|
||||
}
|
||||
return fmt.Sprint(ret+"] ", b.Value())
|
||||
return fmt.Sprint(ret+"(", b.Strength(), ";", b.Length(), ") ]")
|
||||
}
|
||||
|
||||
type Component struct {
|
||||
@@ -169,7 +123,7 @@ func (c *Component) Equals(t Component) bool {
|
||||
return c.Side1 == t.Side1 && c.Side2 == t.Side2
|
||||
}
|
||||
|
||||
func (c *Component) Value() int {
|
||||
func (c *Component) Strength() int {
|
||||
return c.Side1 + c.Side2
|
||||
}
|
||||
|
||||
@@ -184,6 +138,10 @@ func (c *Component) Other(v int) int {
|
||||
return c.Side1
|
||||
}
|
||||
|
||||
func (c *Component) String() string {
|
||||
return c.Name
|
||||
}
|
||||
|
||||
func Atoi(i string) int {
|
||||
var ret int
|
||||
var err error
|
||||
|
Reference in New Issue
Block a user