DEV Community

angeljrp
angeljrp

Posted on

Solving the Chessboard Challenge with Golang

Yesterday, I tackled a coding exercise on exercism.org, and let me tell you, it was quite a brain teaser! The challenge was inspired by a tale of a wise servant who saved a prince's life. In return, the king offered the servant anything he desired, and what did the servant ask for? Grains of wheat, of course!

The task involved two functions:

Square Function:

Section 1: Guarding Against Wild Numbers

// Check if the input is out of bounds
if number <= 0 || number > 64 {
    return 0, errors.New("invalid number")
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

Okay, so we're setting up some rules here. We want to make sure our number is well-behaved, not too small or too big. Think of it like setting boundaries for a friendly game of chess - gotta keep things in check!

Section 2: VIP Treatment for Squares 1 and 2

// Handle squares 1 and 2 as special cases
if number == 1 {
    return 1, nil
} else if number == 2 {
    return 2, nil
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

Now, squares 1 and 2 get the star treatment. They have their own special rules - 1 grain for the first square and 2 grains for the second. It's like giving them the VIP experience without making them wait in line!

Section 3: Doubling the Fun for Other Squares

// Calculate grains for the remaining squares
var lastSquare uint64 = 2
var actualSquare uint64

for i := 3; i <= number; i++ {
    actualSquare = lastSquare * 2
    lastSquare = actualSquare
}
return actualSquare, nil
Enter fullscreen mode Exit fullscreen mode

Explanation:

Now, for the other squares, it's a bit like a math adventure. We start with 2 grains because that's what the second square gets, and then each new square gets double the grains of the last one. It's like grains doing a cool doubling dance – a bit of mathematical magic!

Total Function:

Section 1: Getting the Final Magic Count

// Get the grains on the last square using Square function
n, _ := Square(64)
Enter fullscreen mode Exit fullscreen mode

Explanation:

Alright, imagine we're at the grand finale of our magical chessboard show. We want to know how many grains we've got on the last square, so we call our Square function and ask it nicely to spill the beans on the grains. We store that info in a secret variable called n.

Section 2: Avoiding the Magic Overflow

// Adjust for uint64 limit by subtracting 1, doubling, and adding 1
return ((n - 1) * 2) + 1
Enter fullscreen mode Exit fullscreen mode

Explanation:

Now, here's where things get a bit tricky. Our numbers can get really big, but there's a limit. It's like trying to carry too many groceries in one trip – sometimes you drop one. So, we're doing some math acrobatics to make sure we don't spill our grains. We subtract 1, double it, and add 1 back, just to play it safe and stay within our limits.

In the process, I had a bit of a struggle, especially with the first function. Shoutout to my dad for lending a hand and helping me navigate through it. It's always great to have a coding buddy!

So this is how it would look if we put it all together:

package grains

import (
    "errors"
    "fmt"
    )

func Square(number int) (uint64, error) {
    if number <= 0 || number > 64 {
        return 0, errors.New("invalid number")
    }

    if number == 1{
        return 1,nil
    } else if number == 2{
    return 2,nil
    }

    var lastSquare uint64 = 2
    var actualSquare uint64

    for i := 3; i <= number; i++ {
        actualSquare = lastSquare * 2
        lastSquare = actualSquare
    }

    fmt.Println(actualSquare)

    return actualSquare, nil
}

func Total() uint64 {
   n, _ := Square(64)
    fmt.Println(n)
    return ((n - 1) * 2) + 1
}
Enter fullscreen mode Exit fullscreen mode

And there you have it—my journey through a coding challenge, complete with the highs, the lows, and the eventual triumph. If you've got any insights or feedback, feel free to share. It's always enlightening to see a problem from a different perspective. Thanks for joining me on this coding escapade!

Top comments (1)

Collapse
 
evelynpm profile image
Evelyn Pérez martínez

Very interesting challenge. I like your way of explaining and transmitting ideas