Merge pull request #5 from StevanFreeborn/stevanfreeborn/feat/solve-day-two-part-two
feat: solve day 2 part 2
This commit is contained in:
@@ -16,14 +16,18 @@ Each day's solution is contained in a separate package.
|
||||
|
||||
### Running the Solutions
|
||||
|
||||
![TODO]: Update with instructions
|
||||
To run a specific day's solution you can run the packages tests:
|
||||
|
||||
```pwsh
|
||||
go test ./cmd/01
|
||||
```
|
||||
|
||||
## Challenges
|
||||
|
||||
| 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/) | Part 1 should have been super straight-forward, but some whitespace screwed up my parsing of the final range. |
|
||||
| 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/) | |
|
||||
| 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | |
|
||||
| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | |
|
||||
|
||||
+18
-1
@@ -18,7 +18,24 @@ func SolvePartOne(filePath string) int64 {
|
||||
for r := range strRanges {
|
||||
rng := rnge.From(r)
|
||||
|
||||
for id := range rng.InvalidIds() {
|
||||
for id := range rng.InvalidIdsWithEqualHalves() {
|
||||
total += id
|
||||
}
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
func SolvePartTwo(filePath string) int64 {
|
||||
input := file.ReadAllText(filePath)
|
||||
strRanges := strings.SplitSeq(input, SeparatorCharacter)
|
||||
|
||||
total := int64(0)
|
||||
|
||||
for r := range strRanges {
|
||||
rng := rnge.From(r)
|
||||
|
||||
for id := range rng.InvalidIdsWithTwoOrMoreSeq() {
|
||||
total += id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,3 +25,23 @@ func TestSolvePartOneWithInput(t *testing.T) {
|
||||
t.Errorf("expected %d, got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSolvePartTwoWithExampleInput(t *testing.T) {
|
||||
expected := int64(4_174_379_265)
|
||||
|
||||
result := solution.SolvePartTwo("EXAMPLE.txt")
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("expected %d, got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSolvePartTwoWithInput(t *testing.T) {
|
||||
expected := int64(28858486244)
|
||||
|
||||
result := solution.SolvePartTwo("INPUT.txt")
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("expected %d, got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
+43
-2
@@ -10,7 +10,8 @@ import (
|
||||
const RangeSeparatorCharacter = "-"
|
||||
|
||||
type Range interface {
|
||||
InvalidIds() iter.Seq[int64]
|
||||
InvalidIdsWithEqualHalves() iter.Seq[int64]
|
||||
InvalidIdsWithTwoOrMoreSeq() iter.Seq[int64]
|
||||
Start() int64
|
||||
End() int64
|
||||
}
|
||||
@@ -39,7 +40,7 @@ func (r rnge) End() int64 {
|
||||
return r.end
|
||||
}
|
||||
|
||||
func (r rnge) InvalidIds() iter.Seq[int64] {
|
||||
func (r rnge) InvalidIdsWithEqualHalves() iter.Seq[int64] {
|
||||
return func(yield func(int64) bool) {
|
||||
for id := r.start; id <= r.end; id++ {
|
||||
idStr := strconv.FormatInt(id, 10)
|
||||
@@ -65,3 +66,43 @@ func (r rnge) InvalidIds() iter.Seq[int64] {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r rnge) InvalidIdsWithTwoOrMoreSeq() iter.Seq[int64] {
|
||||
return func(yield func(int64) bool) {
|
||||
for id := r.start; id <= r.end; id++ {
|
||||
idStr := strconv.FormatInt(id, 10)
|
||||
idLength := len(idStr)
|
||||
|
||||
isValid := true
|
||||
|
||||
for pLen := 1; pLen <= idLength/2; pLen++ {
|
||||
v := idLength % pLen
|
||||
repeatedPatterDoesNotFit := v != 0
|
||||
|
||||
if repeatedPatterDoesNotFit {
|
||||
continue
|
||||
}
|
||||
|
||||
pattern := idStr[:pLen]
|
||||
numOfRepeats := idLength / pLen
|
||||
repeatedPattern := strings.Repeat(pattern, numOfRepeats)
|
||||
|
||||
if repeatedPattern != idStr {
|
||||
continue
|
||||
}
|
||||
|
||||
isValid = false
|
||||
}
|
||||
|
||||
if isValid {
|
||||
continue
|
||||
}
|
||||
|
||||
ok := yield(id)
|
||||
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,12 +42,12 @@ func TestEnd(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidIds(t *testing.T) {
|
||||
func TestInvalidIdsWithEqualValues(t *testing.T) {
|
||||
r := rnge.From("1000-1100")
|
||||
expectedInvalidIds := []int64{1010}
|
||||
resultInvalidIds := []int64{}
|
||||
|
||||
for id := range r.InvalidIds() {
|
||||
for id := range r.InvalidIdsWithEqualHalves() {
|
||||
resultInvalidIds = append(resultInvalidIds, id)
|
||||
}
|
||||
|
||||
@@ -55,3 +55,46 @@ func TestInvalidIds(t *testing.T) {
|
||||
t.Errorf("expected invalid IDs %v, got %v", expectedInvalidIds, resultInvalidIds)
|
||||
}
|
||||
}
|
||||
|
||||
type TestCase struct {
|
||||
rnge rnge.Range
|
||||
expectedInvalidIds []int64
|
||||
}
|
||||
|
||||
func TestInvalidIdsWithTwoOrMoreSeq(t *testing.T) {
|
||||
testCases := []TestCase{
|
||||
{
|
||||
rnge: rnge.From("11-22"),
|
||||
expectedInvalidIds: []int64{11, 22},
|
||||
},
|
||||
{
|
||||
rnge: rnge.From("95-115"),
|
||||
expectedInvalidIds: []int64{99, 111},
|
||||
},
|
||||
{
|
||||
rnge: rnge.From("998-1012"),
|
||||
expectedInvalidIds: []int64{999, 1010},
|
||||
},
|
||||
{
|
||||
rnge: rnge.From("1188511880-1188511890"),
|
||||
expectedInvalidIds: []int64{1188511885},
|
||||
},
|
||||
{
|
||||
rnge: rnge.From("222220-222224"),
|
||||
expectedInvalidIds: []int64{222222},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range testCases {
|
||||
resultInvalidIds := []int64{}
|
||||
|
||||
for id := range c.rnge.InvalidIdsWithTwoOrMoreSeq() {
|
||||
resultInvalidIds = append(resultInvalidIds, id)
|
||||
}
|
||||
|
||||
if slices.Equal(resultInvalidIds, c.expectedInvalidIds) == false {
|
||||
t.Errorf("expected invalid IDs %v, got %v", c.expectedInvalidIds, resultInvalidIds)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user