diff --git a/.gitignore b/.gitignore index 06c798b..2211df6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -input.txt +*.txt diff --git a/01/parser.go b/day01/common/parser.go similarity index 75% rename from 01/parser.go rename to day01/common/parser.go index c9ab6f4..1128fc5 100644 --- a/01/parser.go +++ b/day01/common/parser.go @@ -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) diff --git a/01/1.go b/day01/ex1/main.go similarity index 68% rename from 01/1.go rename to day01/ex1/main.go index dddc457..4926acd 100644 --- a/01/1.go +++ b/day01/ex1/main.go @@ -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 } diff --git a/01/2.go b/day01/ex2/main.go similarity index 82% rename from 01/2.go rename to day01/ex2/main.go index ffb6131..c7a2d4d 100644 --- a/01/2.go +++ b/day01/ex2/main.go @@ -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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..670069e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module aoc2022 + +go 1.19