@@ 0,0 1,114 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "sort"
+ "strconv"
+ "strings"
+)
+
+type mt struct{}
+
+type xy struct {
+ x, y int
+}
+
+type sensor struct {
+ p xy
+ d int
+}
+
+func abs(x int) int {
+ if x < 0 {
+ return -x
+ }
+ return x
+}
+
+func main() {
+ // Get command-line options
+ // debug := flag.Bool("debug", false, "show debugging output")
+ flag.Parse()
+
+ // Read the input file
+ if len(flag.Args()) < 1 {
+ panic("No input file given")
+ }
+ input, err := os.ReadFile(flag.Args()[0])
+ if err != nil {
+ panic(err)
+ }
+
+ lines := strings.Split(string(input), "\n")
+ numLines := len(lines) - 1
+ sensors := make([]sensor, 0, numLines)
+ beacons := make(map[xy]mt, numLines)
+ for _, line := range lines[:numLines] {
+ line = strings.ReplaceAll(line, "=", " ")
+ line = strings.ReplaceAll(line, ",", "")
+ line = strings.ReplaceAll(line, ":", "")
+ flds := strings.Fields(line)
+ nums := make([]int, 4)
+ for i, s := range []string{flds[3], flds[5], flds[11], flds[13]} {
+ n, err := strconv.Atoi(s)
+ if err != nil {
+ panic(err)
+ }
+ nums[i] = n
+ }
+ mDist := abs(nums[2]-nums[0]) + abs(nums[3]-nums[1])
+ sensors = append(sensors, sensor{xy{nums[0], nums[1]}, mDist})
+ beacons[xy{nums[2], nums[3]}] = mt{}
+ }
+
+ part1 := 0
+ part2 := 0
+ for y := 0; y <= 4000000; y++ {
+ blocks := make([]xy, 0, numLines)
+ for _, s := range sensors {
+ dist := -1
+ if s.p.y-s.d <= y && y <= s.p.y {
+ dist = y - (s.p.y - s.d)
+ } else if s.p.y < y && y <= s.p.y+s.d {
+ dist = (s.p.y + s.d) - y
+ }
+ if dist > -1 {
+ blocks = append(blocks, xy{s.p.x - dist, s.p.x + dist})
+ }
+ }
+ sort.Slice(blocks, func(i, j int) bool {
+ return blocks[i].x < blocks[j].x
+ })
+ for i := 0; len(blocks) > i+1; {
+ if blocks[i+1].x <= blocks[i].y+1 {
+ blocks[i+1].x = blocks[i].x
+ if blocks[i+1].y < blocks[i].y {
+ blocks[i+1].y = blocks[i].y
+ }
+ blocks = append(blocks[:i], blocks[i+1:]...)
+ } else {
+ part2 = 4000000*(blocks[0].y+1) + y
+ i++
+ }
+ }
+
+ if y == 2000000 {
+ for _, b := range blocks {
+ part1 += (b.y - b.x) + 1
+ }
+ for b := range beacons {
+ if b.y == y {
+ part1--
+ }
+ }
+ }
+ if part1 > 0 && part2 > 0 {
+ break
+ }
+ }
+
+ fmt.Printf("Part 1: %d\n", part1)
+ fmt.Printf("Part 2: %d\n", part2)
+}
@@ 0,0 1,29 @@
+Sensor at x=2557568, y=3759110: closest beacon is at x=2594124, y=3746832
+Sensor at x=2684200, y=1861612: closest beacon is at x=2816974, y=2000000
+Sensor at x=1003362, y=1946094: closest beacon is at x=1972523, y=2563441
+Sensor at x=2142655, y=1481541: closest beacon is at x=1932524, y=967542
+Sensor at x=2796219, y=1955744: closest beacon is at x=2816974, y=2000000
+Sensor at x=3890832, y=1818644: closest beacon is at x=3454717, y=2547103
+Sensor at x=2828842, y=1921726: closest beacon is at x=2816974, y=2000000
+Sensor at x=2065227, y=583957: closest beacon is at x=1932524, y=967542
+Sensor at x=2725784, y=2088998: closest beacon is at x=2816974, y=2000000
+Sensor at x=3574347, y=927734: closest beacon is at x=1932524, y=967542
+Sensor at x=2939312, y=2652370: closest beacon is at x=3454717, y=2547103
+Sensor at x=2495187, y=3681541: closest beacon is at x=2431306, y=3703654
+Sensor at x=2878002, y=2054681: closest beacon is at x=2816974, y=2000000
+Sensor at x=1539310, y=3235516: closest beacon is at x=1972523, y=2563441
+Sensor at x=545413, y=533006: closest beacon is at x=-538654, y=69689
+Sensor at x=1828899, y=3980292: closest beacon is at x=2431306, y=3703654
+Sensor at x=3275729, y=2937931: closest beacon is at x=3454717, y=2547103
+Sensor at x=600131, y=3861189: closest beacon is at x=2431306, y=3703654
+Sensor at x=2089895, y=28975: closest beacon is at x=1932524, y=967542
+Sensor at x=2960402, y=3942666: closest beacon is at x=2594124, y=3746832
+Sensor at x=3785083, y=3905392: closest beacon is at x=2594124, y=3746832
+Sensor at x=1721938, y=1077173: closest beacon is at x=1932524, y=967542
+Sensor at x=2515156, y=3751221: closest beacon is at x=2594124, y=3746832
+Sensor at x=2469423, y=2109095: closest beacon is at x=2816974, y=2000000
+Sensor at x=1776986, y=904092: closest beacon is at x=1932524, y=967542
+Sensor at x=2789294, y=3316115: closest beacon is at x=2594124, y=3746832
+Sensor at x=3538757, y=2695066: closest beacon is at x=3454717, y=2547103
+Sensor at x=2299738, y=2708004: closest beacon is at x=1972523, y=2563441
+Sensor at x=2388366, y=3234346: closest beacon is at x=2431306, y=3703654