package main

import (
	"fmt"
	"os"
	"strings"

	h "git.bullercodeworks.com/brian/adventofcode/helpers"
)

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, err := NewScanner(foundBeacons).getTransformedIfOverlap(wrk)
		if err != nil {
			remaining = append(remaining, wrk)
		} else {
			foundBeacons = append(foundBeacons, s.beacons...)
			foundScannerPositions = append(foundScannerPositions, s.position)
		}
		fmt.Println("  ", foundBeacons)
	}
	fmt.Println("# Part 1")
	fmt.Println(len(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
}
