no this isn't about alexandria ocasio-cortez
1package main
2
3import (
4 "strconv"
5 "strings"
6
7 "tangled.org/evan.jarrett.net/aoc2025/internal/puzzle"
8)
9
10type Tuple struct {
11 x int
12 y int
13}
14
15func (t *Tuple) getArea(o Tuple) int {
16 abs := func(n int) int {
17 if n < 0 {
18 return -n
19 }
20 return n
21 }
22
23 return (abs(t.x-o.x) + 1) * (abs(t.y-o.y) + 1)
24}
25
26func (t Tuple) AreaContainsWall(o Tuple, vertical map[int][]Wall, horizontal map[int][]Wall) bool {
27 minX, maxX := min(t.x, o.x), max(t.x, o.x)
28 minY, maxY := min(t.y, o.y), max(t.y, o.y)
29
30 // Check vertical
31 for x := minX + 1; x < maxX; x++ {
32 for _, w := range vertical[x] {
33 if w.start < maxY && w.end > minY {
34 return true
35 }
36 }
37 }
38
39 // Check horizontal
40 for y := minY + 1; y < maxY; y++ {
41 for _, w := range horizontal[y] {
42 if w.start < maxX && w.end > minX {
43 return true
44 }
45 }
46 }
47
48 return false
49}
50
51type Wall struct {
52 start int
53 end int
54}
55
56type DayNine struct {
57 pairs []Tuple
58}
59
60func (d *DayNine) ParseInput(input string) error {
61 for line := range strings.SplitSeq(strings.TrimSpace(input), "\n") {
62 line = strings.TrimSpace(line)
63 if line == "" {
64 continue
65 }
66 xy := strings.Split(line, ",")
67 x, _ := strconv.Atoi(xy[0])
68 y, _ := strconv.Atoi(xy[1])
69 d.pairs = append(d.pairs, Tuple{x, y})
70 }
71 return nil
72}
73
74func (d *DayNine) Part1() (int, error) {
75 maxArea := 0
76 for i := 0; i < len(d.pairs); i++ {
77 for j := i + 1; j < len(d.pairs); j++ {
78 area := d.pairs[i].getArea(d.pairs[j])
79 maxArea = max(maxArea, area)
80 }
81 }
82 return maxArea, nil
83}
84
85func buildWalls(points []Tuple) (vertical map[int][]Wall, horizontal map[int][]Wall) {
86 vertical = make(map[int][]Wall)
87 horizontal = make(map[int][]Wall)
88
89 for i := 0; i < len(points); i++ {
90 for j := i + 1; j < len(points); j++ {
91 p1, p2 := points[i], points[j]
92 if p1.x == p2.x {
93 // Vertical wall
94 y1, y2 := min(p1.y, p2.y), max(p1.y, p2.y)
95 vertical[p1.x] = append(vertical[p1.x], Wall{y1, y2})
96 } else if p1.y == p2.y {
97 // Horizontal wall
98 x1, x2 := min(p1.x, p2.x), max(p1.x, p2.x)
99 horizontal[p1.y] = append(horizontal[p1.y], Wall{x1, x2})
100 }
101 }
102 }
103 return vertical, horizontal
104}
105
106func (d *DayNine) Part2() (int, error) {
107 vert, horiz := buildWalls(d.pairs)
108 maxArea := 0
109
110 for i := 0; i < len(d.pairs); i++ {
111 for j := i + 1; j < len(d.pairs); j++ {
112 if !d.pairs[i].AreaContainsWall(d.pairs[j], vert, horiz) {
113 maxArea = max(maxArea, d.pairs[i].getArea(d.pairs[j]))
114 }
115 }
116 }
117
118 return maxArea, nil
119}
120
121func main() {
122 puzzle.Run(9, &DayNine{})
123}