diff --git a/README.md b/README.md index 13f4b6c..55ae363 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ go test ./cmd/01 | 01 | [Problem](https://adventofcode.com/2025/day/1) | [Solution](./cmd/01/) | Part 1 seemed straight forward enough. Part 2 completely stumped me. The trickiest part was not counting zero's twice when you ended and then started from there. | | 02 | [Problem](https://adventofcode.com/2025/day/2) | [Solution](./cmd/02/) | Part 1 should have been super straight-forward, but some whitespace screwed up my parsing of the final range. I some how was able to stumble over a solution to part 2 rather blindly. | | 03 | [Problem](https://adventofcode.com/2025/day/3) | [Solution](./cmd/03/) | Part 1 intuitively makes you think you only care about highest nums, but then you need to consider placement. I once again tripped over the solution using a stack. However chat morally supported me the whole time. | -| 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | | +| 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | Part 1 was by far the most straight-forward challenge so far. | | 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | | | 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | | | 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | | diff --git a/cmd/04/main.go b/cmd/04/main.go new file mode 100644 index 0000000..ba53e3e --- /dev/null +++ b/cmd/04/main.go @@ -0,0 +1,82 @@ +package main + +import ( + "github.com/StevanFreeborn/advent-of-code-2025/internal/file" +) + +const PaperRollCharacter = "@" +const EmptyCharacter = "." + +type move struct { + NumberOfRows int + NumberOfColumns int +} + +var ( + up = move{NumberOfRows: -1, NumberOfColumns: 0} + down = move{NumberOfRows: 1, NumberOfColumns: 0} + right = move{NumberOfRows: 0, NumberOfColumns: 1} + left = move{NumberOfRows: 0, NumberOfColumns: -1} + upRight = move{NumberOfRows: -1, NumberOfColumns: 1} + upLeft = move{NumberOfRows: -1, NumberOfColumns: -1} + downRight = move{NumberOfRows: 1, NumberOfColumns: 1} + downLeft = move{NumberOfRows: 1, NumberOfColumns: -1} +) + +var moves = []move{ + up, + down, + right, + left, + upRight, + upLeft, + downRight, + downLeft, +} + +func SolvePartOne(filePath string) int { + input := file.ReadAllLines(filePath) + numberOfRows := len(input) + numberOfColumns := len(input[0]) + + total := 0 + + for row := range numberOfRows { + for column := range numberOfColumns { + value := string(input[row][column]) + + if value != PaperRollCharacter { + continue + } + + numberOfPaperRolls := 0 + + for _, move := range moves { + neighborRow := row + move.NumberOfRows + neighborColumn := column + move.NumberOfColumns + + if neighborRow > numberOfRows-1 || neighborRow < 0 { + continue + } + + if neighborColumn > numberOfColumns-1 || neighborColumn < 0 { + continue + } + + neighborValue := string(input[neighborRow][neighborColumn]) + + if neighborValue != PaperRollCharacter { + continue + } + + numberOfPaperRolls++ + } + + if numberOfPaperRolls < 4 { + total++ + } + } + } + + return total +} diff --git a/cmd/04/main_test.go b/cmd/04/main_test.go new file mode 100644 index 0000000..cb0ee68 --- /dev/null +++ b/cmd/04/main_test.go @@ -0,0 +1,27 @@ +package main_test + +import ( + "testing" + + solution "github.com/StevanFreeborn/advent-of-code-2025/cmd/04" +) + +func TestSolvePartOneWithExampleInput(t *testing.T) { + expected := 13 + + result := solution.SolvePartOne("EXAMPLE.txt") + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +} + +func TestSolvePartOneWithInput(t *testing.T) { + expected := 1395 + + result := solution.SolvePartOne("INPUT.txt") + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +}