Structure
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1 @@
|
||||
input.txt
|
||||
*.txt
|
||||
|
||||
@@ -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)
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user