feat: solve day 2 part 2
This commit is contained in:
+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