package main
import (
"bufio"
"log"
"os"
"strings"
)
type routes map[string][]string
type seen map[string]int
func main() {
if err := myMain(); err != nil {
log.Println(err)
}
}
func myMain() error {
r, err := parseInput("input.txt")
if err != nil {
return err
}
log.Println(solveFirst(r))
return nil
}
func solveFirst(r routes) int {
s := seen{}
return r.genRoutes(s, "start")
}
func (r routes) genRoutes(s seen, root string) int {
var sum int
if root == "end" {
sum++
return sum
}
if s[root] > 0 {
return 0
}
if strings.ToLower(root) == root && root != "end" {
// lowercase, only visit once
s[root]++
}
for _, item := range r[root] {
sum += r.genRoutes(s, item)
}
delete(s, root)
return sum
}
func parseInput(fileName string) (routes, error) {
var ret = routes{}
fd, err := os.Open(fileName)
if err != nil {
return ret, err
}
defer fd.Close()
buf := bufio.NewScanner(fd)
for buf.Scan() {
tmp := strings.Split(buf.Text(), "-")
if tmp[1] != "start" && tmp[0] != "end" {
ret[tmp[0]] = append(ret[tmp[0]], tmp[1])
}
if tmp[0] != "start" && tmp[1] != "end" {
ret[tmp[1]] = append(ret[tmp[1]], tmp[0])
}
}
return ret, nil
}