adventofcode/2017/day24/day24.go

162 lines
2.8 KiB
Go
Raw Normal View History

2017-12-26 14:16:20 +00:00
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
var comps []Component
2017-12-26 20:12:20 +00:00
var strongest, best Bridge
2017-12-26 14:16:20 +00:00
func main() {
inp := StdinToStrings()
for i := range inp {
comps = append(comps, NewComponent(inp[i]))
}
2017-12-26 20:12:20 +00:00
FindStrongest([]Component{NewComponent("0/0")})
fmt.Println("Strongest:\n", strongest)
2017-12-26 14:16:20 +00:00
2017-12-26 20:12:20 +00:00
FindBest([]Component{NewComponent("0/0")})
fmt.Println("Best:\n", best)
}
2017-12-26 14:16:20 +00:00
2017-12-26 20:12:20 +00:00
// Strongest, regardless of length
func FindStrongest(bridge Bridge) {
if bridge.Strength() > strongest.Strength() {
strongest = bridge
2017-12-26 14:16:20 +00:00
}
2017-12-26 20:12:20 +00:00
for i := range comps {
if comps[i].Has(bridge.Needs()) && !bridge.Has(comps[i]) {
FindStrongest(append(bridge, comps[i]))
2017-12-26 14:16:20 +00:00
}
}
}
2017-12-26 20:12:20 +00:00
// 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
2017-12-26 14:16:20 +00:00
}
}
for i := range comps {
2017-12-26 20:12:20 +00:00
if comps[i].Has(bridge.Needs()) && !bridge.Has(comps[i]) {
FindBest(append(bridge, comps[i]))
2017-12-26 14:16:20 +00:00
}
}
}
type Bridge []Component
2017-12-26 20:12:20 +00:00
func (b Bridge) Strength() int {
2017-12-26 14:16:20 +00:00
var ret int
for _, v := range []Component(b) {
2017-12-26 20:12:20 +00:00
ret += v.Strength()
2017-12-26 14:16:20 +00:00
}
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)
}
2017-12-26 20:12:20 +00:00
func (b Bridge) Length() int {
return len([]Component(b))
}
2017-12-26 14:16:20 +00:00
func (b Bridge) String() string {
ret := "[ "
for _, v := range []Component(b) {
ret += v.Name + " "
}
2017-12-26 20:12:20 +00:00
return fmt.Sprint(ret+"(", b.Strength(), ";", b.Length(), ") ]")
2017-12-26 14:16:20 +00:00
}
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
}
2017-12-26 20:12:20 +00:00
func (c *Component) Strength() int {
2017-12-26 14:16:20 +00:00
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
}
2017-12-26 20:12:20 +00:00
func (c *Component) String() string {
return c.Name
}
2017-12-26 14:16:20 +00:00
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
}