Skip to content

Commit

Permalink
refactor: Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko committed Dec 1, 2023
1 parent 5009c65 commit 9c720e5
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions internal/puzzles/solutions/2023/day01/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,41 +103,41 @@ func extractNumberFromLine(line string, dict map[string]int) (int, error) {
var word string

for _, c := range line {
if !unicode.IsDigit(c) {
word += string(c)

if d, ok := getDigitFromWord(word, dict); ok {
if first == -1 {
first = d
} else {
last = d
}
var (
d int
ok bool
err error
)

word = word[len(word)-1:]
}
switch {
case unicode.IsDigit(c):
word = " "

continue
}

word = ""

if first == -1 {
d, err := strconv.Atoi(string(c))
d, err = strconv.Atoi(string(c))
if err != nil {
return 0, fmt.Errorf("failed to convert %q to int: %w", string(c), err)

Check warning on line 118 in internal/puzzles/solutions/2023/day01/solution.go

View check run for this annotation

Codecov / codecov/patch

internal/puzzles/solutions/2023/day01/solution.go#L118

Added line #L118 was not covered by tests
}

first = d
ok = true
case unicode.IsLetter(c):
word += string(c)

continue
d, ok = getDigitFromWord(word, dict)
default:
word = ""

Check warning on line 127 in internal/puzzles/solutions/2023/day01/solution.go

View check run for this annotation

Codecov / codecov/patch

internal/puzzles/solutions/2023/day01/solution.go#L126-L127

Added lines #L126 - L127 were not covered by tests
}

d, err := strconv.Atoi(string(c))
if err != nil {
return 0, fmt.Errorf("failed to convert %q to int: %w", string(c), err)
if !ok {
continue
}

last = d
word = word[len(word)-1:]

if first == -1 {
first = d
} else {
last = d
}
}

if last == -1 {
Expand Down

0 comments on commit 9c720e5

Please sign in to comment.