From 2888719dc56855b6f5858697a62e43a6826b22eb Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Thu, 18 Dec 2025 14:04:21 -0600 Subject: [PATCH 1/2] feat: wip on day 9 part 2 solution --- cmd/09/main.go | 120 +++++++++++++++++++++++++++++----- cmd/09/main_test.go | 20 ++++++ internal/position/position.go | 10 ++- 3 files changed, 133 insertions(+), 17 deletions(-) diff --git a/cmd/09/main.go b/cmd/09/main.go index 4cdda16..726951b 100644 --- a/cmd/09/main.go +++ b/cmd/09/main.go @@ -2,39 +2,35 @@ package main import ( "math" + "slices" "strconv" "strings" "github.com/StevanFreeborn/advent-of-code-2025/internal/file" + "github.com/StevanFreeborn/advent-of-code-2025/internal/position" ) func SolvePartOne(filePath string) int { - points := []struct { - x int - y int - }{} + positions := []position.Position{} for line := range file.ReadLines(filePath) { parts := strings.Split(line, ",") x, _ := strconv.Atoi(parts[0]) y, _ := strconv.Atoi(parts[1]) - p := struct { - x int - y int - }{x: x, y: y} - points = append(points, p) + p := position.From(y, x) + positions = append(positions, p) } - numberOfPoints := len(points) + numberOfPositions := len(positions) largestArea := 0 - for i := range numberOfPoints { - for j := i + 1; j < numberOfPoints; j++ { - start := points[i] - end := points[j] + for i := range numberOfPositions { + for j := i + 1; j < numberOfPositions; j++ { + start := positions[i] + end := positions[j] - width := int(math.Abs(float64(end.x-start.x)) + 1) - height := int(math.Abs(float64(end.y-start.y)) + 1) + width := int(math.Abs(float64(end.Column()-start.Column())) + 1) + height := int(math.Abs(float64(end.Row()-start.Row())) + 1) a := width * height @@ -46,3 +42,95 @@ func SolvePartOne(filePath string) int { return largestArea } + +// TODO: How would we construct all the positions +// in the polygon? + +func SolvePartTwo(filePath string) int { + positions := []position.Position{} + + for line := range file.ReadLines(filePath) { + parts := strings.Split(line, ",") + x, _ := strconv.Atoi(parts[0]) + y, _ := strconv.Atoi(parts[1]) + p := position.From(y, x) + positions = append(positions, p) + } + + rowPositions := []int{} + columnPositions := []int{} + + for _, positions := range positions { + rowPositions = append(rowPositions, positions.Row()) + columnPositions = append(columnPositions, positions.Column()) + } + + minRow := slices.Min(rowPositions) + maxRow := slices.Max(rowPositions) + minCol := slices.Min(columnPositions) + maxCol := slices.Max(columnPositions) + + numberOfPositions := len(positions) + largestArea := 0 + + for i := range numberOfPositions { + for j := i + 1; j < numberOfPositions; j++ { + start := positions[i] + end := positions[j] + + width := int(math.Abs(float64(end.Column()-start.Column())) + 1) + height := int(math.Abs(float64(end.Row()-start.Row())) + 1) + + a := width * height + + if a <= largestArea { + continue + } + + rectMinRow := math.Min(float64(start.Row()), float64(end.Row())) + rectMaxRow := math.Max(float64(start.Row()), float64(end.Row())) + rectMinCol := math.Min(float64(start.Column()), float64(end.Column())) + rectMaxCol := math.Max(float64(start.Column()), float64(end.Column())) + + // is center of rect inside our polygon? + centerRow := int((rectMaxRow + rectMinRow) / 2) + centerCol := int((rectMaxCol + rectMinCol) / 2) + center := position.From(centerRow, centerCol) + + centerIsInside := false + j := numberOfPositions - 1 + + for i := 0; i < numberOfPositions; i++ { + edgeStart := positions[i] + edgeEnd := positions[j] + + edgeCoversCenterRow := (edgeStart.Row() > center.Row()) != (edgeEnd.Row() > center.Row()) + + edgeHorizontalDistance := edgeEnd.Column() - edgeStart.Column() + edgeVeritcalDistance := edgeEnd.Row() - edgeStart.Row() + centerVerticalOffset := center.Row() - edgeStart.Row() + verticalDistanceCompleted := centerVerticalOffset / edgeVeritcalDistance + xIntersection := center.Column() + edgeHorizontalDistance*verticalDistanceCompleted + edgeIsToRightOfCenter := center.Column() < xIntersection + + if edgeCoversCenterRow && edgeIsToRightOfCenter { + centerIsInside = !centerIsInside + } + + j = i + } + + if centerIsInside == false { + continue + } + + // TODO: We need to figure this out + // if edges of polygon intersect with + // edges of rectangle + + largestArea = a + } + } + + return largestArea +} diff --git a/cmd/09/main_test.go b/cmd/09/main_test.go index 405ac26..dfee36e 100644 --- a/cmd/09/main_test.go +++ b/cmd/09/main_test.go @@ -25,3 +25,23 @@ func TestSolvePartOneWithInput(t *testing.T) { t.Errorf("got %d but wanted %d", result, expected) } } + +func TestSolvePartTwoWithExampleInput(t *testing.T) { + expected := 24 + + result := solution.SolvePartTwo("EXAMPLE.txt") + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +} + +func TestSolvePartTwoWithInput(t *testing.T) { + expected := -1 + + result := solution.SolvePartTwo("INPUT.txt") + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +} diff --git a/internal/position/position.go b/internal/position/position.go index af5e679..4c22d23 100644 --- a/internal/position/position.go +++ b/internal/position/position.go @@ -1,7 +1,11 @@ // Package position provides types and functions to represent and manipulate positions and moves on a 2D grid. package position -import "github.com/StevanFreeborn/advent-of-code-2025/internal/move" +import ( + "fmt" + + "github.com/StevanFreeborn/advent-of-code-2025/internal/move" +) type Position interface { Row() int @@ -32,3 +36,7 @@ func (p position) Column() int { func (p position) Move(move move.Move) Position { return From(p.row+move.NumberOfRows(), p.column+move.NumberOfColumns()) } + +func (p position) String() string { + return fmt.Sprintf("c: %d r: %d", p.column, p.row) +} From 7910fcf93229dc01719dc03308285fd43ecfc58a Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Fri, 19 Dec 2025 06:28:32 -0600 Subject: [PATCH 2/2] feat: solve day 9 part 2 --- README.md | 2 +- cmd/09/main.go | 101 ++++++++++++++++++++++++++------------------ cmd/09/main_test.go | 2 +- 3 files changed, 63 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index ac3a21e..a786aa6 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ go test ./cmd/01 | 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | Part 1 I don't think it is the best solution, but it does solve it. I kicked ass on part 2. 🥳 | | 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | FIFO for life. 🤣. Fuck me...part 2 I found really difficult and the use of DFS was obvious, but doing the memoizing was not. | | 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | Part 1 had me down bad. I was on the right track with my original solution, but ended up doing it in a brute force way then figuring out my mistake with the original. Fuck this guy. Part 2 was so much easier once part 1 was sorted out. | -| 09 | [Problem](https://adventofcode.com/2025/day/9) | [Solution](./cmd/09/) | Part 1 was such a nice break from the madness of day 8. | +| 09 | [Problem](https://adventofcode.com/2025/day/9) | [Solution](./cmd/09/) | Part 1 was such a nice break from the madness of day 8. Basically I need to go build some video games. | | 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | | | 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | | | 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | | diff --git a/cmd/09/main.go b/cmd/09/main.go index 726951b..e30d9c7 100644 --- a/cmd/09/main.go +++ b/cmd/09/main.go @@ -2,7 +2,6 @@ package main import ( "math" - "slices" "strconv" "strings" @@ -43,8 +42,7 @@ func SolvePartOne(filePath string) int { return largestArea } -// TODO: How would we construct all the positions -// in the polygon? +// TODO: Refactor below so more understandable func SolvePartTwo(filePath string) int { positions := []position.Position{} @@ -57,19 +55,6 @@ func SolvePartTwo(filePath string) int { positions = append(positions, p) } - rowPositions := []int{} - columnPositions := []int{} - - for _, positions := range positions { - rowPositions = append(rowPositions, positions.Row()) - columnPositions = append(columnPositions, positions.Column()) - } - - minRow := slices.Min(rowPositions) - maxRow := slices.Max(rowPositions) - minCol := slices.Min(columnPositions) - maxCol := slices.Max(columnPositions) - numberOfPositions := len(positions) largestArea := 0 @@ -78,8 +63,13 @@ func SolvePartTwo(filePath string) int { start := positions[i] end := positions[j] - width := int(math.Abs(float64(end.Column()-start.Column())) + 1) - height := int(math.Abs(float64(end.Row()-start.Row())) + 1) + rectMinRow := int(math.Min(float64(start.Row()), float64(end.Row()))) + rectMaxRow := int(math.Max(float64(start.Row()), float64(end.Row()))) + rectMinCol := int(math.Min(float64(start.Column()), float64(end.Column()))) + rectMaxCol := int(math.Max(float64(start.Column()), float64(end.Column()))) + + width := int(math.Abs(float64(rectMaxCol-rectMinCol)) + 1) + height := int(math.Abs(float64(rectMaxRow-rectMinRow)) + 1) a := width * height @@ -87,46 +77,77 @@ func SolvePartTwo(filePath string) int { continue } - rectMinRow := math.Min(float64(start.Row()), float64(end.Row())) - rectMaxRow := math.Max(float64(start.Row()), float64(end.Row())) - rectMinCol := math.Min(float64(start.Column()), float64(end.Column())) - rectMaxCol := math.Max(float64(start.Column()), float64(end.Column())) - - // is center of rect inside our polygon? centerRow := int((rectMaxRow + rectMinRow) / 2) centerCol := int((rectMaxCol + rectMinCol) / 2) center := position.From(centerRow, centerCol) centerIsInside := false - j := numberOfPositions - 1 - for i := 0; i < numberOfPositions; i++ { + for i := range numberOfPositions { edgeStart := positions[i] - edgeEnd := positions[j] + edgeEndIndex := (i + 1) % numberOfPositions + edgeEnd := positions[edgeEndIndex] - edgeCoversCenterRow := (edgeStart.Row() > center.Row()) != (edgeEnd.Row() > center.Row()) + isCenterVerticallyBetweenEdge := (edgeStart.Row() <= center.Row()) && (center.Row() <= edgeEnd.Row()) - edgeHorizontalDistance := edgeEnd.Column() - edgeStart.Column() - edgeVeritcalDistance := edgeEnd.Row() - edgeStart.Row() - centerVerticalOffset := center.Row() - edgeStart.Row() - verticalDistanceCompleted := centerVerticalOffset / edgeVeritcalDistance - xIntersection := center.Column() + edgeHorizontalDistance*verticalDistanceCompleted - edgeIsToRightOfCenter := center.Column() < xIntersection + edgeTotalHeight := int(math.Abs(float64(edgeEnd.Row() - edgeStart.Row()))) + edgeTotalWidth := int(math.Abs(float64(edgeEnd.Column() - edgeStart.Column()))) + centerPercentageOfEdgeHeight := int(float64(center.Row()-edgeStart.Row()) / float64(edgeTotalHeight)) + edgeColumnAtCenterRow := edgeStart.Column() + centerPercentageOfEdgeHeight*edgeTotalWidth + isCenterHorizontallyToTheLeft := center.Row() <= edgeColumnAtCenterRow - if edgeCoversCenterRow && edgeIsToRightOfCenter { + if isCenterVerticallyBetweenEdge && isCenterHorizontallyToTheLeft { centerIsInside = !centerIsInside } - - j = i } if centerIsInside == false { continue } - // TODO: We need to figure this out - // if edges of polygon intersect with - // edges of rectangle + isIntersectedByEdge := false + + for i := range numberOfPositions { + currPos := positions[i] + nextPosIndex := (i + 1) % numberOfPositions + nextPos := positions[nextPosIndex] + + if currPos.Column() == nextPos.Column() { + col := currPos.Column() + + if col > rectMinCol && col < rectMaxCol { + edgeMinRow := math.Min(float64(currPos.Row()), float64(nextPos.Row())) + edgeMaxRow := math.Max(float64(currPos.Row()), float64(nextPos.Row())) + + overlapStart := int(math.Max(edgeMinRow, float64(rectMinRow))) + overlapEnd := int(math.Min(edgeMaxRow, float64(rectMaxRow))) + + if overlapEnd > overlapStart { + isIntersectedByEdge = true + break + } + } + } else { + row := currPos.Row() + + if row > rectMinRow && row < rectMaxRow { + edgeMinCol := math.Min(float64(currPos.Column()), float64(nextPos.Column())) + edgeMaxCol := math.Max(float64(currPos.Column()), float64(nextPos.Column())) + + overlapStart := int(math.Max(edgeMinCol, float64(rectMinCol))) + overlapEnd := int(math.Min(edgeMaxCol, float64(rectMaxCol))) + + if overlapEnd > overlapStart { + isIntersectedByEdge = true + break + } + } + } + } + + if isIntersectedByEdge { + continue + } largestArea = a } diff --git a/cmd/09/main_test.go b/cmd/09/main_test.go index dfee36e..a3586f0 100644 --- a/cmd/09/main_test.go +++ b/cmd/09/main_test.go @@ -37,7 +37,7 @@ func TestSolvePartTwoWithExampleInput(t *testing.T) { } func TestSolvePartTwoWithInput(t *testing.T) { - expected := -1 + expected := 1_577_956_170 result := solution.SolvePartTwo("INPUT.txt")