From 9c221a81121ea58492675328c28ab6a91676e2d4 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Tue, 9 Dec 2025 07:09:27 -0600 Subject: [PATCH] feat: solve day 5 part 1 --- README.md | 2 +- cmd/02/rnge/rnge.go | 3 ++ cmd/05/main.go | 87 +++++++++++++++++++++++++++++++++++++++++++++ cmd/05/main_test.go | 27 ++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 cmd/05/main.go create mode 100644 cmd/05/main_test.go diff --git a/README.md b/README.md index 15150ae..c6b1f05 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ go test ./cmd/01 | 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/) | 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/) | | +| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | Part 1 was fairly straight forward. Merging ranges was trickiest bit. | | 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | | | 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | | | 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | | diff --git a/cmd/02/rnge/rnge.go b/cmd/02/rnge/rnge.go index 4e72d78..8e416fc 100644 --- a/cmd/02/rnge/rnge.go +++ b/cmd/02/rnge/rnge.go @@ -7,6 +7,9 @@ import ( "strings" ) +// TODO: Refactor this to separate package +// in internal/ + const RangeSeparatorCharacter = "-" type Range interface { diff --git a/cmd/05/main.go b/cmd/05/main.go new file mode 100644 index 0000000..23830a8 --- /dev/null +++ b/cmd/05/main.go @@ -0,0 +1,87 @@ +package main + +import ( + "cmp" + "math" + "slices" + "strconv" + "strings" + + "github.com/StevanFreeborn/advent-of-code-2025/internal/file" +) + +const RangeSeparatorCharacter = "-" + +type rnge struct { + start int64 + end int64 +} + +func NewRange(rngeStr string) rnge { + parts := strings.Split(strings.TrimSpace(rngeStr), RangeSeparatorCharacter) + start, _ := strconv.ParseInt(parts[0], 10, 64) + end, _ := strconv.ParseInt(parts[1], 10, 64) + + return rnge{ + start: start, + end: end, + } +} + +func SolvePartOne(filePath string) int { + input := file.ReadAllLines(filePath) + + parseIds := false + + ids := []int64{} + ranges := []rnge{} + + for _, line := range input { + if line == "" { + parseIds = true + } + + if parseIds { + id, _ := strconv.ParseInt(line, 10, 64) + ids = append(ids, id) + continue + } + + rnge := NewRange(line) + ranges = append(ranges, rnge) + } + + slices.SortFunc(ranges, func(a rnge, b rnge) int { + return cmp.Compare(a.start, b.start) + }) + + mergedRanges := []rnge{ + ranges[0], + } + + for _, currentRange := range ranges { + lastMergedRange := mergedRanges[len(mergedRanges)-1] + + if currentRange.start > lastMergedRange.end { + mergedRanges = append(mergedRanges, currentRange) + continue + } + + lastMergedRange.end = int64(math.Max(float64(lastMergedRange.end), float64(currentRange.end))) + mergedRanges[len(mergedRanges)-1] = lastMergedRange + } + + total := 0 + + for _, id := range ids { + for _, rnge := range ranges { + if id >= rnge.start && id <= rnge.end { + total++ + break + } + } + + } + + return total +} diff --git a/cmd/05/main_test.go b/cmd/05/main_test.go new file mode 100644 index 0000000..92f16b7 --- /dev/null +++ b/cmd/05/main_test.go @@ -0,0 +1,27 @@ +package main_test + +import ( + "testing" + + solution "github.com/StevanFreeborn/advent-of-code-2025/cmd/05" +) + +func TestSolvePartOneWithExampleInput(t *testing.T) { + expected := 3 + + result := solution.SolvePartOne("EXAMPLE.txt") + + if result != expected { + t.Errorf("SolvePartOne with example input: expected %d, got %d", expected, result) + } +} + +func TestSolvePartOneWithInput(t *testing.T) { + expected := 643 + + result := solution.SolvePartOne("INPUT.txt") + + if result != expected { + t.Errorf("SolvePartOne with input: expected %d, got %d", expected, result) + } +}