More work on 2021 day 19

This commit is contained in:
Brian Buller 2021-12-30 06:13:04 -06:00
parent 9bc3b7300b
commit 3bf29e69ae

View File

@ -1,7 +1,6 @@
package main
import (
"errors"
"fmt"
"os"
"strings"
@ -12,21 +11,29 @@ import (
func main() {
inp := h.StdinToStringSlice()
scanners := parseScanners(inp)
part1(scanners)
}
func part1(scanners []scanner) {
foundBeacons := scanners[0].beacons
foundScannerPositions := []beacon{{0, 0, 0}}
remaining := scanners[1:]
for len(remaining) > 0 {
fmt.Println(len(remaining), "Remaining")
var wrk scanner
wrk, remaining = remaining[0], remaining[1:]
s := NewScanner(foundBeacons).getScannerFromOverlap(wrk)
if s == nil {
remaining = append(remaining, s)
s, err := NewScanner(foundBeacons).getTransformedIfOverlap(wrk)
if err != nil {
remaining = append(remaining, wrk)
} else {
foundBeacons = append(foundBeacons, s.beacons...)
//foundScannerPositions = append(foundScannerPositions,
foundScannerPositions = append(foundScannerPositions, s.position)
}
fmt.Println(" ", foundBeacons)
}
fmt.Println("# Part 1")
fmt.Println(len(foundScannerPositions))
}
func parseScanners(inp []string) []scanner {
@ -52,61 +59,3 @@ func parseScanners(inp []string) []scanner {
}
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)
}