diff --git a/README.md b/README.md index 75fd6d4..15ed6ee 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Each day's solution is contained in a separate package. | Day | Problem | Solution | Notes | |-----|-------------------------------------------------|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 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/) | | +| 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. | | 03 | [Problem](https://adventofcode.com/2025/day/3) | [Solution](./cmd/03/) | | | 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | | | 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | | diff --git a/cmd/02/main.go b/cmd/02/main.go new file mode 100644 index 0000000..6ed345d --- /dev/null +++ b/cmd/02/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "os" + "strconv" + "strings" +) + +const SeparatorCharacter = "," +const RangeSeparatorCharacter = "-" + +type Range struct { + Start int64 + End int64 +} + +func SolvePartOne(filePath string) int64 { + bytes, readErr := os.ReadFile(filePath) + + if readErr != nil { + return 0 + } + + strRanges := strings.SplitSeq(string(bytes), SeparatorCharacter) + + rngs := []Range{} + + for r := range strRanges { + parts := strings.Split(strings.TrimSpace(r), "-") + start, _ := strconv.ParseInt(parts[0], 10, 64) + end, _ := strconv.ParseInt(parts[1], 10, 64) + rng := Range{ + Start: start, + End: end, + } + rngs = append(rngs, rng) + } + + total := int64(0) + + for _, rng := range rngs { + for id := rng.Start; id <= rng.End; id++ { + idStr := strconv.FormatInt(id, 10) + idLength := len(idStr) + + if idLength%2 != 0 { + continue + } + + midpoint := idLength / 2 + + firstHalf := idStr[:midpoint] + secondHalf := idStr[midpoint:] + + if firstHalf != secondHalf { + continue + } + + total += id + } + } + + return total +} diff --git a/cmd/02/main_test.go b/cmd/02/main_test.go new file mode 100644 index 0000000..7b0f9ad --- /dev/null +++ b/cmd/02/main_test.go @@ -0,0 +1,27 @@ +package main_test + +import ( + "testing" + + solution "github.com/StevanFreeborn/advent-of-code-2025/cmd/02" +) + +func TestSolvePartOneWithExampleInput(t *testing.T) { + expected := int64(1_227_775_554) + + result := solution.SolvePartOne("EXAMPLE.txt") + + if result != expected { + t.Errorf("expected %d, got %d", expected, result) + } +} + +func TestSolvePartOneWithInput(t *testing.T) { + expected := int64(18_952_700_150) + + result := solution.SolvePartOne("INPUT.txt") + + if result != expected { + t.Errorf("expected %d, got %d", expected, result) + } +}