Day 1
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
input.txt
|
||||||
21
01/1.go
Normal file
21
01/1.go
Normal 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
30
01/2.go
Normal 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
41
01/parser.go
Normal 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user