From 0f1ee8c5999d964debef5f7f12b15dc9de8029af Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 4 Dec 2023 13:10:03 +0000 Subject: [PATCH] refactor(Day 3): updated Day 3 solution code - added a custom function to check if a rune is a symbol. - changed name of type 'answer' to 'solution'. - ci(forgejo actions): updated setup-go from v3 to v4. --- .forgejo/workflows/workflow.yaml | 2 +- 2023/day-3/main.go | 30 +++++++++++++++++------------- 2023/day-3/main_test.go | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/.forgejo/workflows/workflow.yaml b/.forgejo/workflows/workflow.yaml index a695fe2..a45b35b 100644 --- a/.forgejo/workflows/workflow.yaml +++ b/.forgejo/workflows/workflow.yaml @@ -13,7 +13,7 @@ jobs: - name: Checkout Repository uses: https://code.forgejo.org/actions/checkout@v3 - name: Setup Go - uses: https://code.forgejo.org/actions/setup-go@v3 + uses: https://code.forgejo.org/actions/setup-go@v4 with: go-version: '1.21' - name: Test diff --git a/2023/day-3/main.go b/2023/day-3/main.go index ae435a7..eac8a23 100644 --- a/2023/day-3/main.go +++ b/2023/day-3/main.go @@ -14,7 +14,7 @@ const ( potentialGear = '*' ) -type answer struct { +type solution struct { sumOfValidEnginePartNumbers int sumOfGearRatios int } @@ -34,7 +34,7 @@ func main() { fmt.Printf("[Part 2] The sum of all Gear Ratios: %d\n", answer.sumOfGearRatios) } -func fixTheGondola(schematic []string) (answer, error) { +func fixTheGondola(schematic []string) (solution, error) { var ( partNumSum = 0 partNum = "" @@ -52,49 +52,49 @@ func fixTheGondola(schematic []string) (answer, error) { if !validPartNum { switch { // check above left - case ((y - 1) >= 0) && ((x - 1) >= 0) && !unicode.IsDigit(rune(schematic[y-1][x-1])) && (schematic[y-1][x-1] != dot): + case ((y - 1) >= 0) && ((x - 1) >= 0) && isSymbol(rune(schematic[y-1][x-1])): validPartNum = true if schematic[y-1][x-1] == potentialGear { potentialGearPos = fmt.Sprintf("%d-%d", y-1, x-1) } // check straight above - case ((y - 1) >= 0) && !unicode.IsDigit(rune(schematic[y-1][x])) && (schematic[y-1][x] != dot): + case ((y - 1) >= 0) && isSymbol(rune(schematic[y-1][x])): validPartNum = true if schematic[y-1][x] == potentialGear { potentialGearPos = fmt.Sprintf("%d-%d", y-1, x) } // check above right - case ((y - 1) >= 0) && ((x + 1) <= maxX) && !unicode.IsDigit(rune(schematic[y-1][x+1])) && (schematic[y-1][x+1] != dot): + case ((y - 1) >= 0) && ((x + 1) <= maxX) && isSymbol(rune(schematic[y-1][x+1])): validPartNum = true if schematic[y-1][x+1] == potentialGear { potentialGearPos = fmt.Sprintf("%d-%d", y-1, x+1) } // check left - case ((x - 1) >= 0) && !unicode.IsDigit(rune(schematic[y][x-1])) && (schematic[y][x-1] != dot): + case ((x - 1) >= 0) && isSymbol(rune(schematic[y][x-1])): validPartNum = true if schematic[y][x-1] == potentialGear { potentialGearPos = fmt.Sprintf("%d-%d", y, x-1) } // check right - case ((x + 1) <= maxX) && !unicode.IsDigit(rune(schematic[y][x+1])) && (schematic[y][x+1] != dot): + case ((x + 1) <= maxX) && isSymbol(rune(schematic[y][x+1])): validPartNum = true if schematic[y][x+1] == potentialGear { potentialGearPos = fmt.Sprintf("%d-%d", y, x+1) } // check below left - case ((y + 1) <= maxY) && ((x - 1) >= 0) && !unicode.IsDigit(rune(schematic[y+1][x-1])) && (schematic[y+1][x-1] != dot): + case ((y + 1) <= maxY) && ((x - 1) >= 0) && isSymbol(rune(schematic[y+1][x-1])): validPartNum = true if schematic[y+1][x-1] == potentialGear { potentialGearPos = fmt.Sprintf("%d-%d", y+1, x-1) } // check straight below - case ((y + 1) <= maxY) && !unicode.IsDigit(rune(schematic[y+1][x])) && (schematic[y+1][x] != dot): + case ((y + 1) <= maxY) && isSymbol(rune(schematic[y+1][x])): validPartNum = true if schematic[y+1][x] == potentialGear { potentialGearPos = fmt.Sprintf("%d-%d", y+1, x) } // check below right - case ((y + 1) <= maxY) && ((x + 1) <= maxX) && !unicode.IsDigit(rune(schematic[y+1][x+1])) && (schematic[y+1][x+1] != dot): + case ((y + 1) <= maxY) && ((x + 1) <= maxX) && isSymbol(rune(schematic[y+1][x+1])): validPartNum = true if schematic[y+1][x+1] == potentialGear { potentialGearPos = fmt.Sprintf("%d-%d", y+1, x+1) @@ -105,7 +105,7 @@ func fixTheGondola(schematic []string) (answer, error) { if validPartNum { value, err := strconv.Atoi(partNum) if err != nil { - return answer{}, fmt.Errorf("unable to convert %q to int; %w", partNum, err) + return solution{}, fmt.Errorf("unable to convert %q to int; %w", partNum, err) } partNumSum = partNumSum + value @@ -130,12 +130,12 @@ func fixTheGondola(schematic []string) (answer, error) { } } - ans := answer{ + answer := solution{ sumOfValidEnginePartNumbers: partNumSum, sumOfGearRatios: sumGearRatios, } - return ans, nil + return answer, nil } func engineSchematic(filename string) ([]string, error) { @@ -153,3 +153,7 @@ func engineSchematic(filename string) ([]string, error) { return lines, nil } + +func isSymbol(r rune) bool { + return !unicode.IsDigit(r) && (r != dot) +} diff --git a/2023/day-3/main_test.go b/2023/day-3/main_test.go index 11589af..74134c8 100644 --- a/2023/day-3/main_test.go +++ b/2023/day-3/main_test.go @@ -36,7 +36,7 @@ func testFixTheGondola(schematic []string) func(t *testing.T) { t.Fatalf("Received an error after running sumOfValidEnginePartNumbers(); %v\n", err) } - want := answer{ + want := solution{ sumOfValidEnginePartNumbers: 4361, sumOfGearRatios: 467835, }