Merge pull request #3 from StevanFreeborn/stevanfreeborn/feat/solve-day-two-part-one
feat: solve day 2 part 1
This commit is contained in:
@@ -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/) | |
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user