adventofcode/2021/day19/main.go

113 lines
2.6 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
scanners := parseScanners(inp)
foundBeacons := scanners[0].beacons
foundScannerPositions := []beacon{{0, 0, 0}}
remaining := scanners[1:]
for len(remaining) > 0 {
var wrk scanner
wrk, remaining = remaining[0], remaining[1:]
s := NewScanner(foundBeacons).getScannerFromOverlap(wrk)
if s == nil {
remaining = append(remaining, s)
} else {
foundBeacons = append(foundBeacons, s.beacons...)
//foundScannerPositions = append(foundScannerPositions,
}
}
}
func parseScanners(inp []string) []scanner {
var scanners []scanner
for i := range inp {
if strings.HasPrefix(inp[i], "---") {
// Find the next empty line
end := i
for ; end < len(inp); end++ {
if inp[end] == "" {
break
}
}
// a new scanner
wrk, err := NewScannerFromInput(inp[i:end])
if err == nil {
scanners = append(scanners, wrk)
} else {
fmt.Println("Error parsing scanners")
os.Exit(1)
}
}
}
return scanners
}
type scanner struct {
number int
region map[h.Coordinate]bool
beacons []beacon
}
func NewScanner(b []beacon) scanner {
return scanner{beacons: b}
}
func NewScannerFromInput(inp []string) (scanner, error) {
s := scanner{}
r := strings.NewReader(inp[0])
_, err := fmt.Fscanf(r, "--- scanner %d ---", &s.number)
if err != nil {
return s, errors.New("No Scanner ID")
}
for _, v := range inp[1:] {
r = strings.NewReader(v)
b := beacon{}
_, err := fmt.Fscanf(r, "%d,%d,%d", &b.x, &b.y, &b.z)
if err == nil {
s.beacons = append(s.beacons, b)
}
}
return s, nil
}
func (s scanner) allRotations() [][]beacon {
var beacons [][]beacon
for i := range s.beacons {
beacons = append(beacons, s.beacons[i].allRotations())
}
return beacons
}
func (s scanner) String() string {
return fmt.Sprintf("-- Scanner %d (%d beacons)---", s.number, len(s.beacons))
}
type beacon struct {
x, y, z int
}
func (b beacon) allRotations() []beacon {
return []beacon{
{b.x, b.y, b.z}, {b.x, -b.z, b.y}, {b.x, -b.y, -b.z}, {b.x, b.z, -b.y}, {-b.x, -b.y, b.z},
{-b.x, -b.z, -b.y}, {-b.x, b.y, -b.z}, {-b.x, b.z, b.y}, {-b.z, b.x, -b.y}, {b.y, b.x, -b.z},
{b.z, b.x, b.y}, {-b.y, b.x, b.z}, {b.z, -b.x, -b.y}, {b.y, -b.x, b.z}, {-b.z, -b.x, b.y},
{-b.y, -b.x, -b.z}, {-b.y, -b.z, b.x}, {b.z, -b.y, b.x}, {b.y, b.z, b.x}, {-b.z, b.y, b.x},
{b.z, b.y, -b.x}, {-b.y, b.z, -b.x}, {-b.z, -b.y, -b.x}, {b.y, -b.z, -b.x},
}
}
func (b beacon) String() string {
return fmt.Sprintf("{%d,%d,%d}", b.x, b.y, b.z)
}