I wanted to use something like a right-hand wall solver, but the fact that you don't know the maze ahead of time and you can't see what something is without trying to move into it made that difficult. This semi-brute-force approach works well enough. I originally stopped as soon as I found the oxygen system and figured out the shortest path, but once I submitted that answer and saw that part 2 wanted the full map explored, I figured I might as well just do it all at once. Part 2 might be able to use the right-hand exploration rule since it has the full map, maybe...possibly a pathfinding/A* type solution, but the problem is finding the "goal" location (furthest point from the oxygen system) itself, so I'm not sure if those will work. My current plan is to either try right-hand wall walking or some sort of breadth-first tree system to plot all distances from the oxygen system, then take the furthest one as the answer. I think I would have been stuck on part 1 longer if my input set didn't happen to find the goal system fairly easily (or maybe my debug drawing helped me work through it with that input set specifically, I'm not sure) since a different input set required some tweaking to the max-visited threshold in order to find things that my first input set found with a lower setting. Regardless, I'm pretty excited that I came to Trémaux's algorithm, more or less, on my own. I went to Wikipedia to see if I was on the right track and lo and behold, I came to a version of it myself.
126 lines
2.5 KiB
Go
126 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"parnic.com/aoc2019/days"
|
|
"parnic.com/aoc2019/utilities"
|
|
)
|
|
|
|
type day interface {
|
|
Parse()
|
|
Num() int
|
|
Part1() string
|
|
Part2() string
|
|
}
|
|
|
|
const (
|
|
part1Header = utilities.ColorGreen + "Part1:" + utilities.TextReset
|
|
part2Header = utilities.ColorGreen + "Part2:" + utilities.TextReset
|
|
)
|
|
|
|
var (
|
|
flagPart1 = flag.Bool("part1", false, "whether to run part1 or not; if no flags are present, all parts are run")
|
|
flagPart2 = flag.Bool("part2", false, "whether to run part2 or not; if no flags are present, all parts are run")
|
|
)
|
|
|
|
var dayMap = []day{
|
|
&days.Day01{},
|
|
&days.Day02{},
|
|
&days.Day03{},
|
|
&days.Day04{},
|
|
&days.Day05{},
|
|
&days.Day06{},
|
|
&days.Day07{},
|
|
&days.Day08{},
|
|
&days.Day09{},
|
|
&days.Day10{},
|
|
&days.Day11{},
|
|
&days.Day12{},
|
|
&days.Day13{},
|
|
&days.Day14{},
|
|
&days.Day15{},
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
arg := strconv.Itoa(len(dayMap))
|
|
flagArgs := flag.Args()
|
|
if len(flagArgs) > 0 && len(flagArgs[0]) > 0 {
|
|
arg = flagArgs[0]
|
|
}
|
|
if strings.ToLower(arg) == "all" {
|
|
startTime := time.Now()
|
|
for _, v := range dayMap {
|
|
solve(v)
|
|
}
|
|
fmt.Printf("%sAll days completed in %v%s\n", utilities.ColorBrightBlack, time.Since(startTime), utilities.TextReset)
|
|
} else {
|
|
iArg, err := strconv.Atoi(arg)
|
|
if err != nil {
|
|
log.Fatalf("Invalid day " + utilities.ColorCyan + arg + utilities.TextReset)
|
|
}
|
|
|
|
if iArg < 0 || iArg > len(dayMap) {
|
|
log.Fatalf("Unknown day " + utilities.ColorCyan + arg + utilities.TextReset)
|
|
}
|
|
|
|
solve(dayMap[iArg-1])
|
|
}
|
|
|
|
os.Exit(0)
|
|
}
|
|
|
|
func solve(d day) {
|
|
fmt.Printf("%sDay %d%s\n", utilities.ColorCyan, d.Num(), utilities.TextReset)
|
|
fmt.Printf("----%s\n", strings.Repeat("-", len(strconv.Itoa(d.Num()))))
|
|
|
|
runPart1 := (!*flagPart1 && !*flagPart2) || *flagPart1
|
|
runPart2 := (!*flagPart1 && !*flagPart2) || *flagPart2
|
|
|
|
parseStart := time.Now()
|
|
d.Parse()
|
|
parseTime := time.Since(parseStart)
|
|
|
|
part1Start := time.Now()
|
|
var part1Text string
|
|
if runPart1 {
|
|
part1Text = d.Part1()
|
|
}
|
|
part1Time := time.Since(part1Start)
|
|
|
|
part2Start := time.Now()
|
|
var part2Text string
|
|
if runPart2 {
|
|
part2Text = d.Part2()
|
|
}
|
|
part2Time := time.Since(part2Start)
|
|
|
|
if runPart1 {
|
|
fmt.Println(part1Header)
|
|
fmt.Println(">", part1Text)
|
|
fmt.Println()
|
|
}
|
|
if runPart2 {
|
|
fmt.Println(part2Header)
|
|
fmt.Println(">", part2Text)
|
|
fmt.Println()
|
|
}
|
|
fmt.Print(utilities.ColorBrightBlack)
|
|
fmt.Println("Parsed in", parseTime)
|
|
if runPart1 {
|
|
fmt.Println("Part01 in", part1Time)
|
|
}
|
|
if runPart2 {
|
|
fmt.Println("Part02 in", part2Time)
|
|
}
|
|
fmt.Println(utilities.TextReset)
|
|
}
|