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.
This commit is contained in:
parent
5af35d0ccc
commit
0f1ee8c599
3 changed files with 19 additions and 15 deletions
|
@ -13,7 +13,7 @@ jobs:
|
||||||
- name: Checkout Repository
|
- name: Checkout Repository
|
||||||
uses: https://code.forgejo.org/actions/checkout@v3
|
uses: https://code.forgejo.org/actions/checkout@v3
|
||||||
- name: Setup Go
|
- name: Setup Go
|
||||||
uses: https://code.forgejo.org/actions/setup-go@v3
|
uses: https://code.forgejo.org/actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: '1.21'
|
go-version: '1.21'
|
||||||
- name: Test
|
- name: Test
|
||||||
|
|
|
@ -14,7 +14,7 @@ const (
|
||||||
potentialGear = '*'
|
potentialGear = '*'
|
||||||
)
|
)
|
||||||
|
|
||||||
type answer struct {
|
type solution struct {
|
||||||
sumOfValidEnginePartNumbers int
|
sumOfValidEnginePartNumbers int
|
||||||
sumOfGearRatios int
|
sumOfGearRatios int
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ func main() {
|
||||||
fmt.Printf("[Part 2] The sum of all Gear Ratios: %d\n", answer.sumOfGearRatios)
|
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 (
|
var (
|
||||||
partNumSum = 0
|
partNumSum = 0
|
||||||
partNum = ""
|
partNum = ""
|
||||||
|
@ -52,49 +52,49 @@ func fixTheGondola(schematic []string) (answer, error) {
|
||||||
if !validPartNum {
|
if !validPartNum {
|
||||||
switch {
|
switch {
|
||||||
// check above left
|
// 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
|
validPartNum = true
|
||||||
if schematic[y-1][x-1] == potentialGear {
|
if schematic[y-1][x-1] == potentialGear {
|
||||||
potentialGearPos = fmt.Sprintf("%d-%d", y-1, x-1)
|
potentialGearPos = fmt.Sprintf("%d-%d", y-1, x-1)
|
||||||
}
|
}
|
||||||
// check straight above
|
// 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
|
validPartNum = true
|
||||||
if schematic[y-1][x] == potentialGear {
|
if schematic[y-1][x] == potentialGear {
|
||||||
potentialGearPos = fmt.Sprintf("%d-%d", y-1, x)
|
potentialGearPos = fmt.Sprintf("%d-%d", y-1, x)
|
||||||
}
|
}
|
||||||
// check above right
|
// 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
|
validPartNum = true
|
||||||
if schematic[y-1][x+1] == potentialGear {
|
if schematic[y-1][x+1] == potentialGear {
|
||||||
potentialGearPos = fmt.Sprintf("%d-%d", y-1, x+1)
|
potentialGearPos = fmt.Sprintf("%d-%d", y-1, x+1)
|
||||||
}
|
}
|
||||||
// check left
|
// 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
|
validPartNum = true
|
||||||
if schematic[y][x-1] == potentialGear {
|
if schematic[y][x-1] == potentialGear {
|
||||||
potentialGearPos = fmt.Sprintf("%d-%d", y, x-1)
|
potentialGearPos = fmt.Sprintf("%d-%d", y, x-1)
|
||||||
}
|
}
|
||||||
// check right
|
// 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
|
validPartNum = true
|
||||||
if schematic[y][x+1] == potentialGear {
|
if schematic[y][x+1] == potentialGear {
|
||||||
potentialGearPos = fmt.Sprintf("%d-%d", y, x+1)
|
potentialGearPos = fmt.Sprintf("%d-%d", y, x+1)
|
||||||
}
|
}
|
||||||
// check below left
|
// 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
|
validPartNum = true
|
||||||
if schematic[y+1][x-1] == potentialGear {
|
if schematic[y+1][x-1] == potentialGear {
|
||||||
potentialGearPos = fmt.Sprintf("%d-%d", y+1, x-1)
|
potentialGearPos = fmt.Sprintf("%d-%d", y+1, x-1)
|
||||||
}
|
}
|
||||||
// check straight below
|
// 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
|
validPartNum = true
|
||||||
if schematic[y+1][x] == potentialGear {
|
if schematic[y+1][x] == potentialGear {
|
||||||
potentialGearPos = fmt.Sprintf("%d-%d", y+1, x)
|
potentialGearPos = fmt.Sprintf("%d-%d", y+1, x)
|
||||||
}
|
}
|
||||||
// check below right
|
// 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
|
validPartNum = true
|
||||||
if schematic[y+1][x+1] == potentialGear {
|
if schematic[y+1][x+1] == potentialGear {
|
||||||
potentialGearPos = fmt.Sprintf("%d-%d", y+1, x+1)
|
potentialGearPos = fmt.Sprintf("%d-%d", y+1, x+1)
|
||||||
|
@ -105,7 +105,7 @@ func fixTheGondola(schematic []string) (answer, error) {
|
||||||
if validPartNum {
|
if validPartNum {
|
||||||
value, err := strconv.Atoi(partNum)
|
value, err := strconv.Atoi(partNum)
|
||||||
if err != nil {
|
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
|
partNumSum = partNumSum + value
|
||||||
|
|
||||||
|
@ -130,12 +130,12 @@ func fixTheGondola(schematic []string) (answer, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ans := answer{
|
answer := solution{
|
||||||
sumOfValidEnginePartNumbers: partNumSum,
|
sumOfValidEnginePartNumbers: partNumSum,
|
||||||
sumOfGearRatios: sumGearRatios,
|
sumOfGearRatios: sumGearRatios,
|
||||||
}
|
}
|
||||||
|
|
||||||
return ans, nil
|
return answer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func engineSchematic(filename string) ([]string, error) {
|
func engineSchematic(filename string) ([]string, error) {
|
||||||
|
@ -153,3 +153,7 @@ func engineSchematic(filename string) ([]string, error) {
|
||||||
|
|
||||||
return lines, nil
|
return lines, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isSymbol(r rune) bool {
|
||||||
|
return !unicode.IsDigit(r) && (r != dot)
|
||||||
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ func testFixTheGondola(schematic []string) func(t *testing.T) {
|
||||||
t.Fatalf("Received an error after running sumOfValidEnginePartNumbers(); %v\n", err)
|
t.Fatalf("Received an error after running sumOfValidEnginePartNumbers(); %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
want := answer{
|
want := solution{
|
||||||
sumOfValidEnginePartNumbers: 4361,
|
sumOfValidEnginePartNumbers: 4361,
|
||||||
sumOfGearRatios: 467835,
|
sumOfGearRatios: 467835,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue