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
|
||||
}
|
||||
22
day01/ex1/main.go
Normal file
22
day01/ex1/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"aoc2022/day01/common"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inventories := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||
|
||||
largestInventoryTotal := 0
|
||||
for _, inventory := range inventories {
|
||||
currentTotal := inventory.GetTotal()
|
||||
if currentTotal > largestInventoryTotal {
|
||||
largestInventoryTotal = currentTotal
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(largestInventoryTotal)
|
||||
}
|
||||
31
day01/ex2/main.go
Normal file
31
day01/ex2/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"aoc2022/day01/common"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inventories := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||
|
||||
largestInventoriesTotal := []int{0, 0, 0}
|
||||
largestInventoriesIndex := []int{-1, -1, -1}
|
||||
for i := 0; i < 3; i++ {
|
||||
for index, inventory := range inventories {
|
||||
currentTotal := inventory.GetTotal()
|
||||
if currentTotal > largestInventoriesTotal[i] && index != largestInventoriesIndex[0] && index != largestInventoriesIndex[1] {
|
||||
largestInventoriesTotal[i] = currentTotal
|
||||
largestInventoriesIndex[i] = index
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sum := 0
|
||||
for _, inventoryTotal := range largestInventoriesTotal {
|
||||
sum += inventoryTotal
|
||||
}
|
||||
|
||||
fmt.Println(sum)
|
||||
}
|
||||
Reference in New Issue
Block a user