From 41d26d1a230f4cd7ade55f31e1d6b9f618c9cc0c Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sat, 13 Dec 2025 06:08:15 -0600 Subject: [PATCH] feat: solve day 7 part 1 --- README.md | 2 +- cmd/07/main.go | 76 +++++++++++++++++++++++++++++++++++++++++++++ cmd/07/main_test.go | 27 ++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 cmd/07/main.go create mode 100644 cmd/07/main_test.go diff --git a/README.md b/README.md index 12fb65c..3c7a07f 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ go test ./cmd/01 | 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | Part 1 was by far the most straight-forward challenge so far. This was easiest day of AoC this year by far. | | 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | Part 1 was fairly straight forward. Merging ranges was trickiest bit. I feel like I'm being led into a trap with how straight forward today was. | | 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/) | | +| 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | FIFO for life. 🤣 | | 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | | | 09 | [Problem](https://adventofcode.com/2025/day/9) | [Solution](./cmd/09/) | | | 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | | diff --git a/cmd/07/main.go b/cmd/07/main.go new file mode 100644 index 0000000..3b3ab83 --- /dev/null +++ b/cmd/07/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "github.com/StevanFreeborn/advent-of-code-2025/internal/file" + "github.com/StevanFreeborn/advent-of-code-2025/internal/position" +) + +const StartCharacter = "S" +const SplitterCharacter = "^" + +func SolvePartOne(filePath string) int { + input := file.ReadAllLines(filePath) + numOfRows := len(input) + numOfCols := len(input[0]) + + startRow := -1 + startCol := -1 + + for row := range numOfRows - 1 { + for col := range numOfCols - 1 { + v := string(input[row][col]) + + if v == StartCharacter { + startRow = row + startCol = col + break + } + } + } + + if startRow < 0 || startCol < 0 { + panic("unable to find start location") + } + + beamStart := position.From(startRow+1, startCol) + beamsQueue := []position.Position{ + beamStart, + } + visited := map[position.Position]bool{} + + total := 0 + + for len(beamsQueue) > 0 { + currentBeam := beamsQueue[0] + beamsQueue = beamsQueue[1:] + + if visited[currentBeam] { + continue + } + + if currentBeam.Row() < 0 || currentBeam.Row() >= numOfRows { + continue + } + + if currentBeam.Column() < 0 || currentBeam.Column() >= numOfCols { + continue + } + + visited[currentBeam] = true + + value := string(input[currentBeam.Row()][currentBeam.Column()]) + + if value == SplitterCharacter { + total++ + rightBeam := position.From(currentBeam.Row(), currentBeam.Column()+1) + leftBeam := position.From(currentBeam.Row(), currentBeam.Column()-1) + beamsQueue = append(beamsQueue, rightBeam, leftBeam) + continue + } + + nextBeam := position.From(currentBeam.Row()+1, currentBeam.Column()) + beamsQueue = append(beamsQueue, nextBeam) + } + + return total +} diff --git a/cmd/07/main_test.go b/cmd/07/main_test.go new file mode 100644 index 0000000..b8b49bc --- /dev/null +++ b/cmd/07/main_test.go @@ -0,0 +1,27 @@ +package main_test + +import ( + "testing" + + solution "github.com/StevanFreeborn/advent-of-code-2025/cmd/07" +) + +func TestSolvePartOneWithExampleInput(t *testing.T) { + expected := 21 + + result := solution.SolvePartOne("EXAMPLE.txt") + + if result != expected { + t.Errorf("SolvePartOne() = %d; want %d", result, expected) + } +} + +func TestSolvePartOneWithInput(t *testing.T) { + expected := 1581 + + result := solution.SolvePartOne("INPUT.txt") + + if result != expected { + t.Errorf("SolvePartOne() = %d; want %d", result, expected) + } +}