feat: solve day 5 part 1

This commit is contained in:
Stevan Freeborn
2025-12-09 07:09:27 -06:00
parent 247f76026d
commit 9c221a8112
4 changed files with 118 additions and 1 deletions
+1 -1
View File
@@ -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. | | 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. | | 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. | | 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/) | | | 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | |
| 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | | | 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | |
| 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | | | 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | |
+3
View File
@@ -7,6 +7,9 @@ import (
"strings" "strings"
) )
// TODO: Refactor this to separate package
// in internal/
const RangeSeparatorCharacter = "-" const RangeSeparatorCharacter = "-"
type Range interface { type Range interface {
+87
View File
@@ -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
}
+27
View File
@@ -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)
}
}