DEV Community

Discussion on: Daily Challenge #58 - Smelting Iron Ingots

Collapse
 
dak425 profile image
Donald Feury

Time to Go smelting!

ingots.go

package ingots

const (
    lavaDuration     int = 800
    blazeRodDuration int = 120
    coalDuration     int = 80
    woodDuration     int = 15
)

// Requirements represents the differents types of fuel and in what amounts needed to complete the smelting job
type Requirements struct {
    Lava     int
    BlazeRod int
    Coal     int
    Wood     int
    Stick    int
}

// Fuel determines the fuel requirements to smelt the given number of ingots
func Fuel(ingots int) Requirements {
    if ingots <= 0 {
        return Requirements{0, 0, 0, 0, 0}
    }

    duration := ingots * 11

    return Requirements{
        Lava:     duration/lavaDuration + 1,
        BlazeRod: duration/blazeRodDuration + 1,
        Coal:     duration/coalDuration + 1,
        Wood:     duration/woodDuration + 1,
        Stick:    duration,
    }
}

ingots_test.go

package ingots

import "testing"

func TestFuel(t *testing.T) {
    testCases := []struct {
        description string
        input       int
        expected    Requirements
    }{
        {
            "a few ingots",
            2,
            Requirements{1, 1, 1, 2, 22},
        },
        {
            "wow that is alot of ingots",
            500,
            Requirements{7, 46, 69, 367, 5500},
        },
        {
            "negative amount of ingots",
            -5,
            Requirements{0, 0, 0, 0, 0},
        },
        {
            "no ingots",
            0,
            Requirements{0, 0, 0, 0, 0},
        },
    }

    for _, test := range testCases {
        if result := Fuel(test.input); result != test.expected {
            t.Fatalf("FAIL: %s - Fuel(%d): %+v - expected: %+v", test.description, test.input, result, test.expected)
        }
        t.Logf("PASS: %s", test.description)
    }
}