adventofcode/2021/day19/main.go

62 lines
1.3 KiB
Go
Raw Normal View History

2021-12-20 20:56:51 +00:00
package main
2021-12-21 17:14:16 +00:00
import (
"fmt"
"os"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
2021-12-20 20:56:51 +00:00
func main() {
2021-12-21 17:14:16 +00:00
inp := h.StdinToStringSlice()
scanners := parseScanners(inp)
2021-12-30 12:13:04 +00:00
part1(scanners)
}
func part1(scanners []scanner) {
2021-12-21 17:14:16 +00:00
foundBeacons := scanners[0].beacons
foundScannerPositions := []beacon{{0, 0, 0}}
remaining := scanners[1:]
for len(remaining) > 0 {
2021-12-30 12:13:04 +00:00
fmt.Println(len(remaining), "Remaining")
2021-12-21 17:14:16 +00:00
var wrk scanner
wrk, remaining = remaining[0], remaining[1:]
2021-12-30 12:13:04 +00:00
s, err := NewScanner(foundBeacons).getTransformedIfOverlap(wrk)
if err != nil {
remaining = append(remaining, wrk)
2021-12-21 17:14:16 +00:00
} else {
foundBeacons = append(foundBeacons, s.beacons...)
2021-12-30 12:13:04 +00:00
foundScannerPositions = append(foundScannerPositions, s.position)
2021-12-21 17:14:16 +00:00
}
2021-12-30 12:13:04 +00:00
fmt.Println(" ", foundBeacons)
2021-12-21 17:14:16 +00:00
}
2021-12-30 12:13:04 +00:00
fmt.Println("# Part 1")
fmt.Println(len(foundScannerPositions))
2021-12-21 17:14:16 +00:00
}
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
}