Files
advent-of-code-2022/day01/common/parser.go
2022-12-01 07:51:14 +01:00

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
}