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 4cdda16..e30d9c7 100644 --- a/cmd/09/main.go +++ b/cmd/09/main.go @@ -6,35 +6,30 @@ import ( "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 +41,117 @@ func SolvePartOne(filePath string) int { return largestArea } + +// TODO: Refactor below so more understandable + +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) + } + + numberOfPositions := len(positions) + largestArea := 0 + + for i := range numberOfPositions { + for j := i + 1; j < numberOfPositions; j++ { + start := positions[i] + end := positions[j] + + 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 + + if a <= largestArea { + continue + } + + centerRow := int((rectMaxRow + rectMinRow) / 2) + centerCol := int((rectMaxCol + rectMinCol) / 2) + center := position.From(centerRow, centerCol) + + centerIsInside := false + + for i := range numberOfPositions { + edgeStart := positions[i] + edgeEndIndex := (i + 1) % numberOfPositions + edgeEnd := positions[edgeEndIndex] + + isCenterVerticallyBetweenEdge := (edgeStart.Row() <= center.Row()) && (center.Row() <= edgeEnd.Row()) + + 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 isCenterVerticallyBetweenEdge && isCenterHorizontallyToTheLeft { + centerIsInside = !centerIsInside + } + } + + if centerIsInside == false { + continue + } + + 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 + } + } + + return largestArea +} diff --git a/cmd/09/main_test.go b/cmd/09/main_test.go index 405ac26..a3586f0 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_577_956_170 + + 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) +}