Compare commits

...

2 commits

Author SHA1 Message Date
904a2919f6
tests: add missing test suite descriptions 2023-12-04 23:19:53 +00:00
ec8824682d
refactor(Day 1): replace recursive calls with loop
For Part 2, the recursive function calls are replaced with a for loop in
the form of a while loop.
2023-12-04 23:07:45 +00:00
3 changed files with 20 additions and 18 deletions

View file

@ -6,6 +6,7 @@ import (
)
func TestDay1Trebuchet(t *testing.T) {
t.Logf("This is the test suite for Advent of Code - Day 1 - Trebuchet?!")
t.Run("Test the solution for Part 1", testPartOneCalculateSumCalibrationValues)
t.Run("Test the solution for Part 2", testPartTwoCalculateSumCalibrationValues)
}

View file

@ -17,62 +17,61 @@ const (
)
func partTwoCalculateSumCalibrationValues(document []string) int {
var (
digits []int
sum int
)
sum := 0
for _, line := range document {
sum = sum + partTwoCalibrationValue(line, digits)
sum = sum + partTwoCalibrationValue(line)
}
return sum
}
func partTwoCalibrationValue(line string, digits []int) int {
if len(line) > 0 {
func partTwoCalibrationValue(line string) int {
var digits []int
for len(line) > 0 {
switch {
case (line[0] >= '0') && (line[0] <= '9'):
digits = append(digits, int(line[0]-'0'))
return partTwoCalibrationValue(line[1:], digits)
line = line[1:]
case strings.HasPrefix(line, one):
digits = append(digits, 1)
trimLength := len(one) - 1
return partTwoCalibrationValue(line[trimLength:], digits)
line = line[trimLength:]
case strings.HasPrefix(line, two):
digits = append(digits, 2)
trimLength := len(two) - 1
return partTwoCalibrationValue(line[trimLength:], digits)
line = line[trimLength:]
case strings.HasPrefix(line, three):
digits = append(digits, 3)
trimLength := len(three) - 1
return partTwoCalibrationValue(line[trimLength:], digits)
line = line[trimLength:]
case strings.HasPrefix(line, four):
digits = append(digits, 4)
trimLength := len(four) - 1
return partTwoCalibrationValue(line[trimLength:], digits)
line = line[trimLength:]
case strings.HasPrefix(line, five):
digits = append(digits, 5)
trimLength := len(five) - 1
return partTwoCalibrationValue(line[trimLength:], digits)
line = line[trimLength:]
case strings.HasPrefix(line, six):
digits = append(digits, 6)
trimLength := len(six) - 1
return partTwoCalibrationValue(line[trimLength:], digits)
line = line[trimLength:]
case strings.HasPrefix(line, seven):
digits = append(digits, 7)
trimLength := len(seven) - 1
return partTwoCalibrationValue(line[trimLength:], digits)
line = line[trimLength:]
case strings.HasPrefix(line, eight):
digits = append(digits, 8)
trimLength := len(eight) - 1
return partTwoCalibrationValue(line[trimLength:], digits)
line = line[trimLength:]
case strings.HasPrefix(line, nine):
digits = append(digits, 9)
trimLength := len(nine) - 1
return partTwoCalibrationValue(line[trimLength:], digits)
line = line[trimLength:]
default:
return partTwoCalibrationValue(line[1:], digits)
line = line[1:]
}
}

View file

@ -6,6 +6,8 @@ import (
)
func TestDay2CubeConundrum(t *testing.T) {
t.Logf("This is the test suite for Advent of Code - Day 2 - Cube Conundrum")
testGames := `
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue