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

View File

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

View File

@@ -4,16 +4,17 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"aoc2022/day01/common"
) )
func main() { func main() {
inventories := parse(*bufio.NewScanner(os.Stdin)) inventories := common.Parse(*bufio.NewScanner(os.Stdin))
largestInventoriesTotal := []int{0, 0, 0} largestInventoriesTotal := []int{0, 0, 0}
largestInventoriesIndex := []int{-1, -1, -1} largestInventoriesIndex := []int{-1, -1, -1}
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
for index, inventory := range inventories { for index, inventory := range inventories {
currentTotal := inventory.getTotal() currentTotal := inventory.GetTotal()
if currentTotal > largestInventoriesTotal[i] && index != largestInventoriesIndex[0] && index != largestInventoriesIndex[1] { if currentTotal > largestInventoriesTotal[i] && index != largestInventoriesIndex[0] && index != largestInventoriesIndex[1] {
largestInventoriesTotal[i] = currentTotal largestInventoriesTotal[i] = currentTotal
largestInventoriesIndex[i] = index largestInventoriesIndex[i] = index

3
go.mod Normal file
View File

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