This commit is contained in:
2022-12-01 07:10:11 +01:00
commit d4eb4a757c
4 changed files with 93 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
input.txt

21
01/1.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
inventories := parse(*bufio.NewScanner(os.Stdin))
largestInventoryTotal := 0
for _, inventory := range inventories {
currentTotal := inventory.getTotal()
if currentTotal > largestInventoryTotal {
largestInventoryTotal = currentTotal
}
}
fmt.Println(largestInventoryTotal)
}

30
01/2.go Normal file
View File

@@ -0,0 +1,30 @@
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
inventories := 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)
}

41
01/parser.go Normal file
View File

@@ -0,0 +1,41 @@
package main
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
}