Structure

This commit is contained in:
2022-12-01 07:50:35 +01:00
parent d4eb4a757c
commit 9fe1abe08c
5 changed files with 15 additions and 10 deletions

2
.gitignore vendored
View File

@@ -1 +1 @@
input.txt
*.txt

View File

@@ -1,4 +1,4 @@
package main
package common
import (
"bufio"
@@ -9,11 +9,11 @@ type Inventory struct {
rations []int
}
func (inventory *Inventory) addRation(ration int) {
func (inventory *Inventory) AddRation(ration int) {
inventory.rations = append(inventory.rations, ration)
}
func (inventory Inventory) getTotal() int {
func (inventory Inventory) GetTotal() int {
total := 0
for _, ration := range inventory.rations {
total += ration
@@ -21,7 +21,7 @@ func (inventory Inventory) getTotal() int {
return total
}
func parse(scanner bufio.Scanner) []Inventory {
func Parse(scanner bufio.Scanner) []Inventory {
inventories := []Inventory{}
currentInventory := Inventory{}
@@ -32,7 +32,7 @@ func parse(scanner bufio.Scanner) []Inventory {
currentInventory = Inventory{[]int{}}
} else {
ration, _ := strconv.Atoi(line)
currentInventory.addRation(ration)
currentInventory.AddRation(ration)
}
}
inventories = append(inventories, currentInventory)

View File

@@ -4,14 +4,15 @@ import (
"bufio"
"fmt"
"os"
"aoc2022/day01/common"
)
func main() {
inventories := parse(*bufio.NewScanner(os.Stdin))
inventories := common.Parse(*bufio.NewScanner(os.Stdin))
largestInventoryTotal := 0
for _, inventory := range inventories {
currentTotal := inventory.getTotal()
currentTotal := inventory.GetTotal()
if currentTotal > largestInventoryTotal {
largestInventoryTotal = currentTotal
}

View File

@@ -4,16 +4,17 @@ import (
"bufio"
"fmt"
"os"
"aoc2022/day01/common"
)
func main() {
inventories := parse(*bufio.NewScanner(os.Stdin))
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()
currentTotal := inventory.GetTotal()
if currentTotal > largestInventoriesTotal[i] && index != largestInventoriesIndex[0] && index != largestInventoriesIndex[1] {
largestInventoriesTotal[i] = currentTotal
largestInventoriesIndex[i] = index

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module aoc2022
go 1.19