feat: wip on day 9 part 2 solution
This commit is contained in:
+104
-16
@@ -2,39 +2,35 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SolvePartOne(filePath string) int {
|
func SolvePartOne(filePath string) int {
|
||||||
points := []struct {
|
positions := []position.Position{}
|
||||||
x int
|
|
||||||
y int
|
|
||||||
}{}
|
|
||||||
|
|
||||||
for line := range file.ReadLines(filePath) {
|
for line := range file.ReadLines(filePath) {
|
||||||
parts := strings.Split(line, ",")
|
parts := strings.Split(line, ",")
|
||||||
x, _ := strconv.Atoi(parts[0])
|
x, _ := strconv.Atoi(parts[0])
|
||||||
y, _ := strconv.Atoi(parts[1])
|
y, _ := strconv.Atoi(parts[1])
|
||||||
p := struct {
|
p := position.From(y, x)
|
||||||
x int
|
positions = append(positions, p)
|
||||||
y int
|
|
||||||
}{x: x, y: y}
|
|
||||||
points = append(points, p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
numberOfPoints := len(points)
|
numberOfPositions := len(positions)
|
||||||
largestArea := 0
|
largestArea := 0
|
||||||
|
|
||||||
for i := range numberOfPoints {
|
for i := range numberOfPositions {
|
||||||
for j := i + 1; j < numberOfPoints; j++ {
|
for j := i + 1; j < numberOfPositions; j++ {
|
||||||
start := points[i]
|
start := positions[i]
|
||||||
end := points[j]
|
end := positions[j]
|
||||||
|
|
||||||
width := int(math.Abs(float64(end.x-start.x)) + 1)
|
width := int(math.Abs(float64(end.Column()-start.Column())) + 1)
|
||||||
height := int(math.Abs(float64(end.y-start.y)) + 1)
|
height := int(math.Abs(float64(end.Row()-start.Row())) + 1)
|
||||||
|
|
||||||
a := width * height
|
a := width * height
|
||||||
|
|
||||||
@@ -46,3 +42,95 @@ func SolvePartOne(filePath string) int {
|
|||||||
|
|
||||||
return largestArea
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,3 +25,23 @@ func TestSolvePartOneWithInput(t *testing.T) {
|
|||||||
t.Errorf("got %d but wanted %d", result, expected)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
// Package position provides types and functions to represent and manipulate positions and moves on a 2D grid.
|
// Package position provides types and functions to represent and manipulate positions and moves on a 2D grid.
|
||||||
package position
|
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 {
|
type Position interface {
|
||||||
Row() int
|
Row() int
|
||||||
@@ -32,3 +36,7 @@ func (p position) Column() int {
|
|||||||
func (p position) Move(move move.Move) Position {
|
func (p position) Move(move move.Move) Position {
|
||||||
return From(p.row+move.NumberOfRows(), p.column+move.NumberOfColumns())
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user