Skip to content

Commit de13788

Browse files
committed
Initial commit, day 1 in go.
0 parents  commit de13788

File tree

5 files changed

+2355
-0
lines changed

5 files changed

+2355
-0
lines changed

01/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# --- Day 1: Calorie Counting ---

01/day1.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
)
9+
10+
func main() {
11+
fmt.Println("Part 1: ", part1())
12+
fmt.Println("Part 2: ", part2(3))
13+
}
14+
15+
func part1() int64 {
16+
highestTotal := int64(0)
17+
inventories := parseElvesInventory(readInput("input.txt"))
18+
sumOfInventories := getSumArray(inventories)
19+
20+
for _, total := range sumOfInventories {
21+
if total > highestTotal {
22+
highestTotal = int64(total)
23+
}
24+
}
25+
26+
return highestTotal
27+
}
28+
29+
func part2(n int) int64 {
30+
// Finds n highest numbers in the input
31+
inventories := parseElvesInventory(readInput("input.txt"))
32+
sumOfInventories := getSumArray(inventories)
33+
// Sort sumOfInventories
34+
// Return the first n numbers
35+
36+
for i := 0; i < len(sumOfInventories); i++ {
37+
for j := 0; j < len(sumOfInventories); j++ {
38+
if sumOfInventories[i] > sumOfInventories[j] {
39+
sumOfInventories[i], sumOfInventories[j] = sumOfInventories[j], sumOfInventories[i]
40+
}
41+
}
42+
}
43+
44+
return sum(sumOfInventories[:n])
45+
}
46+
47+
func readInput(inputfile string) []string {
48+
// Read the input file
49+
input, err := os.Open(inputfile)
50+
if err != nil {
51+
panic(err)
52+
}
53+
defer input.Close()
54+
var lines []string
55+
scanner := bufio.NewScanner(input)
56+
for scanner.Scan() {
57+
lines = append(lines, scanner.Text())
58+
}
59+
return lines
60+
}
61+
62+
func parseElvesInventory(input []string) map[int][]int64 {
63+
elvesInventory := make(map[int][]int64)
64+
inventoryIndex := 0
65+
for _, line := range input {
66+
if elvesInventory[inventoryIndex] == nil {
67+
elvesInventory[inventoryIndex] = make([]int64, 0)
68+
}
69+
70+
if line == "" {
71+
inventoryIndex++
72+
continue
73+
}
74+
75+
// Add the new item to the inventory
76+
item, err := strconv.ParseInt(line, 10, 64)
77+
78+
if err != nil {
79+
continue
80+
}
81+
82+
elvesInventory[inventoryIndex] = append(elvesInventory[inventoryIndex], item)
83+
84+
}
85+
return elvesInventory
86+
}
87+
88+
func getSumArray(input map[int][]int64) []int64 {
89+
sumArray := make([]int64, len(input))
90+
for i := 0; i < len(input); i++ {
91+
sumArray[i] = sum(input[i])
92+
}
93+
return sumArray
94+
}
95+
96+
func sum(input []int64) int64 {
97+
sum := int64(0)
98+
for _, item := range input {
99+
sum += item
100+
}
101+
return sum
102+
}

01/input.test.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
1000
2+
2000
3+
3000
4+
5+
4000
6+
7+
5000
8+
6000
9+
10+
7000
11+
8000
12+
9000
13+
14+
10000

0 commit comments

Comments
 (0)