Skip to content

Commit

Permalink
pass another error case
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Dec 5, 2023
1 parent 2d6bd02 commit cb38ce6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 14 additions & 2 deletions go/alphametics/alphametics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ func Solve(input string) (map[string]int, error) {
if err != nil {
return nil, err
}
if equation.nonUniqueValues() {
if equation.onlyTwoUniqueLetters() {
return nil, fmt.Errorf("solution must have unique value for each letter")
}
if equation.addendIsLongerThanSum() {
return nil, fmt.Errorf("leading zero solution is invalid")
}

letters := equation.uniqueLetters()
for {
Expand Down Expand Up @@ -80,10 +83,19 @@ func (e equation) isLeadingZero(letterToNumber map[string]int) bool {
return false
}

func (e equation) nonUniqueValues() bool {
func (e equation) onlyTwoUniqueLetters() bool {
return len(e.uniqueLetters()) == 2
}

func (e equation) addendIsLongerThanSum() bool {
for _, addend := range e.addends {
if len(addend) > len(e.sum) {
return true
}
}
return false
}

func translate(letterToNumber map[string]int, word string) int {
numbers := ""
for _, letter := range word {
Expand Down
10 changes: 5 additions & 5 deletions go/alphametics/cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ var testCases = []struct {
input: "A == B",
errorExpected: true,
},
// {
// description: "leading zero solution is invalid",
// input: "ACA + DD == BD",
// errorExpected: true,
// },
{
description: "leading zero solution is invalid",
input: "ACA + DD == BD",
errorExpected: true,
},
{
description: "puzzle with two digits final carry",
input: "A + A + A + A + A + A + A + A + A + A + A + B == BCC",
Expand Down

0 comments on commit cb38ce6

Please sign in to comment.