Structure
This commit is contained in:
41
day01/common/parser.go
Normal file
41
day01/common/parser.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user