feat: solve day 2 part 2

This commit is contained in:
Stevan Freeborn
2025-12-04 06:33:21 -06:00
parent 267bbd2d81
commit 8c17524cc6
5 changed files with 145 additions and 20 deletions
+43 -2
View File
@@ -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
}
}
}
}