feat: solve day 9 part 2
This commit is contained in:
@@ -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. 🥳 |
|
| 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. |
|
| 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. |
|
| 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/) | |
|
| 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | |
|
||||||
| 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | |
|
| 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | |
|
||||||
| 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | |
|
| 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | |
|
||||||
|
|||||||
+61
-40
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"slices"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -43,8 +42,7 @@ func SolvePartOne(filePath string) int {
|
|||||||
return largestArea
|
return largestArea
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: How would we construct all the positions
|
// TODO: Refactor below so more understandable
|
||||||
// in the polygon?
|
|
||||||
|
|
||||||
func SolvePartTwo(filePath string) int {
|
func SolvePartTwo(filePath string) int {
|
||||||
positions := []position.Position{}
|
positions := []position.Position{}
|
||||||
@@ -57,19 +55,6 @@ func SolvePartTwo(filePath string) int {
|
|||||||
positions = append(positions, p)
|
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)
|
numberOfPositions := len(positions)
|
||||||
largestArea := 0
|
largestArea := 0
|
||||||
|
|
||||||
@@ -78,8 +63,13 @@ func SolvePartTwo(filePath string) int {
|
|||||||
start := positions[i]
|
start := positions[i]
|
||||||
end := positions[j]
|
end := positions[j]
|
||||||
|
|
||||||
width := int(math.Abs(float64(end.Column()-start.Column())) + 1)
|
rectMinRow := int(math.Min(float64(start.Row()), float64(end.Row())))
|
||||||
height := int(math.Abs(float64(end.Row()-start.Row())) + 1)
|
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
|
a := width * height
|
||||||
|
|
||||||
@@ -87,46 +77,77 @@ func SolvePartTwo(filePath string) int {
|
|||||||
continue
|
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)
|
centerRow := int((rectMaxRow + rectMinRow) / 2)
|
||||||
centerCol := int((rectMaxCol + rectMinCol) / 2)
|
centerCol := int((rectMaxCol + rectMinCol) / 2)
|
||||||
center := position.From(centerRow, centerCol)
|
center := position.From(centerRow, centerCol)
|
||||||
|
|
||||||
centerIsInside := false
|
centerIsInside := false
|
||||||
j := numberOfPositions - 1
|
|
||||||
|
|
||||||
for i := 0; i < numberOfPositions; i++ {
|
for i := range numberOfPositions {
|
||||||
edgeStart := positions[i]
|
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()
|
edgeTotalHeight := int(math.Abs(float64(edgeEnd.Row() - edgeStart.Row())))
|
||||||
edgeVeritcalDistance := edgeEnd.Row() - edgeStart.Row()
|
edgeTotalWidth := int(math.Abs(float64(edgeEnd.Column() - edgeStart.Column())))
|
||||||
centerVerticalOffset := center.Row() - edgeStart.Row()
|
centerPercentageOfEdgeHeight := int(float64(center.Row()-edgeStart.Row()) / float64(edgeTotalHeight))
|
||||||
verticalDistanceCompleted := centerVerticalOffset / edgeVeritcalDistance
|
edgeColumnAtCenterRow := edgeStart.Column() + centerPercentageOfEdgeHeight*edgeTotalWidth
|
||||||
xIntersection := center.Column() + edgeHorizontalDistance*verticalDistanceCompleted
|
isCenterHorizontallyToTheLeft := center.Row() <= edgeColumnAtCenterRow
|
||||||
edgeIsToRightOfCenter := center.Column() < xIntersection
|
|
||||||
|
|
||||||
if edgeCoversCenterRow && edgeIsToRightOfCenter {
|
if isCenterVerticallyBetweenEdge && isCenterHorizontallyToTheLeft {
|
||||||
centerIsInside = !centerIsInside
|
centerIsInside = !centerIsInside
|
||||||
}
|
}
|
||||||
|
|
||||||
j = i
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if centerIsInside == false {
|
if centerIsInside == false {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: We need to figure this out
|
isIntersectedByEdge := false
|
||||||
// if edges of polygon intersect with
|
|
||||||
// edges of rectangle
|
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
|
largestArea = a
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -37,7 +37,7 @@ func TestSolvePartTwoWithExampleInput(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSolvePartTwoWithInput(t *testing.T) {
|
func TestSolvePartTwoWithInput(t *testing.T) {
|
||||||
expected := -1
|
expected := 1_577_956_170
|
||||||
|
|
||||||
result := solution.SolvePartTwo("INPUT.txt")
|
result := solution.SolvePartTwo("INPUT.txt")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user