package main import ( "bufio" "fmt" "os" "slices" "strconv" "strings" "time" ) func readFile(fname string) []string { var lines []string file, err := os.Open(fname) if err != nil { fmt.Println(err) } scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() lines = append(lines, line) } return lines } func connectLight(line string, Lights map[int][]int) { line = strings.TrimSpace(line) lightS := strings.Split(line, "-") if len(lightS) < 2 || lightS[1] == "" { return } light, err := strconv.Atoi(lightS[0]) if err != nil { panic(fmt.Sprintf("bad light %q in line %q", lightS[0], line)) } for connectToS := range strings.SplitSeq(lightS[1], ",") { connectTo, _ := strconv.Atoi(strings.TrimSpace(connectToS)) if !slices.Contains(Lights[light], connectTo) { Lights[light] = append(Lights[light], connectTo) } if !slices.Contains(Lights[connectTo], light) { Lights[connectTo] = append(Lights[connectTo], light) } } } func bfs(start int, Lights map[int][]int) map[int]int { dist := map[int]int{start: 0} queue := []int{start} for len(queue) > 0 { current := queue[0] queue = queue[1:] for _, next := range Lights[current] { if _, seen := dist[next]; !seen { dist[next] = dist[current] + 1 queue = append(queue, next) } } } return dist } type point struct{ x, y int } // part2Spec holds where lights 1, 2 and 3 must go, and which light to locate. type part2Spec struct { targets [3]point query int } // orientations lists the 8 ways to rotate/flip a square grid. var orientations = []func(r, c int) point{ func(r, c int) point { return point{r, c} }, func(r, c int) point { return point{c, r} }, func(r, c int) point { return point{-r, c} }, func(r, c int) point { return point{r, -c} }, func(r, c int) point { return point{-r, -c} }, func(r, c int) point { return point{-c, r} }, func(r, c int) point { return point{c, -r} }, func(r, c int) point { return point{-c, -r} }, } func Solve(part2 bool, input []string, spec part2Spec) string { var Lights = make(map[int][]int) for _, line := range input { connectLight(line, Lights) } var dists [4]map[int]int var corners [4]int count := 0 for light, lights := range Lights { if len(lights) == 2 { // OK it's the corner if count == 4 { panic("more than 4 lights with 2 connections: not a square grid") } corners[count] = light dists[count] = bfs(light, Lights) count++ } } if count != 4 { panic(fmt.Sprintf("found %d corners, expected 4", count)) } if !part2 { for light := range dists[0] { if dists[0][light] == dists[1][light] && dists[0][light] == dists[2][light] && dists[0][light] == dists[3][light] { return strconv.Itoa(light) } } return "not found" } distA := dists[0] side := 0 for _, d := range distA { side = max(side, d) } side /= 2 var distB map[int]int for i := 1; i < 4; i++ { if distA[corners[i]] == side { distB = dists[i] break } } local := func(id int) (int, int) { dA, okA := distA[id] dB, okB := distB[id] if !okA || !okB { panic(fmt.Sprintf("light %d is not in the grid", id)) } c := (dA - dB + side) / 2 return dA - c, c } var found func(r, c int) point var offset point matches := 0 for _, orient := range orientations { r1, c1 := local(1) p1 := orient(r1, c1) off := point{spec.targets[0].x - p1.x, spec.targets[0].y - p1.y} ok := true for i, id := range []int{2, 3} { r, c := local(id) p := orient(r, c) if p.x+off.x != spec.targets[i+1].x || p.y+off.y != spec.targets[i+1].y { ok = false break } } if ok { found, offset = orient, off matches++ } } if matches != 1 { panic(fmt.Sprintf("%d orientations match lights 1, 2, 3; expected exactly 1", matches)) } r, c := local(spec.query) p := found(r, c) return fmt.Sprintf("%d,%d", p.x+offset.x, p.y+offset.y) } func main() { timeStart := time.Now() INPUT := "input.txt" // INPUT := "sample.txt" lines := readFile(INPUT) spec := part2Spec{targets: [3]point{{47, 30}, {29, 59}, {56, 35}}, query: 2026} if INPUT == "sample.txt" { spec = part2Spec{targets: [3]point{{11, 20}, {10, 22}, {12, 20}}, query: 7} } fmt.Println("Part1: ", Solve(false, lines, spec)) fmt.Println("Part2: ", Solve(true, lines, spec)) fmt.Printf("Time: %.2fms\n", float64(time.Since(timeStart).Microseconds())/1000) }