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) { 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 1", testPartOneCalculateSumCalibrationValues)
t.Run("Test the solution for Part 2", testPartTwoCalculateSumCalibrationValues) t.Run("Test the solution for Part 2", testPartTwoCalculateSumCalibrationValues)
} }

View file

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

View file

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