42 lines
814 B
Go
42 lines
814 B
Go
package common
|
|
|
|
import (
|
|
"bufio"
|
|
"strconv"
|
|
)
|
|
|
|
type Inventory struct {
|
|
rations []int
|
|
}
|
|
|
|
func (inventory *Inventory) AddRation(ration int) {
|
|
inventory.rations = append(inventory.rations, ration)
|
|
}
|
|
|
|
func (inventory Inventory) GetTotal() int {
|
|
total := 0
|
|
for _, ration := range inventory.rations {
|
|
total += ration
|
|
}
|
|
return total
|
|
}
|
|
|
|
func Parse(scanner bufio.Scanner) []Inventory {
|
|
inventories := []Inventory{}
|
|
currentInventory := Inventory{}
|
|
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if line == "" {
|
|
inventories = append(inventories, currentInventory)
|
|
currentInventory = Inventory{[]int{}}
|
|
} else {
|
|
ration, _ := strconv.Atoi(line)
|
|
currentInventory.AddRation(ration)
|
|
}
|
|
}
|
|
inventories = append(inventories, currentInventory)
|
|
|
|
return inventories
|
|
}
|