Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2fa3b80

Browse filesBrowse files
committed
Day 01 Part 02
1 parent 4dc430b commit 2fa3b80
Copy full SHA for 2fa3b80

File tree

Expand file treeCollapse file tree

2 files changed

+32
-6
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-6
lines changed

‎day01/main.go

Copy file name to clipboardExpand all lines: day01/main.go
+27-6Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,35 @@ import (
1010
//go:embed input2
1111
var input2 string
1212

13+
func partTwo(input string) int {
14+
parsed := parseInput(input)
15+
maxes := findMax(parsed, 3)
16+
sum := sumIntSlice(maxes)
17+
return sum
18+
}
19+
1320
func partOne(input string) int {
1421
parsed := parseInput(input)
15-
max := findMax(parsed)
16-
return max
22+
max := findMax(parsed, 1)
23+
return max[0]
1724
}
1825

19-
func findMax(parsed [][]int) int {
26+
func findMax(parsed [][]int, topN int) []int {
27+
maxes := make([]int, topN)
2028
sums := make([]int, len(parsed))
2129
for i, perElf := range parsed {
22-
// fmt.Printf("%d %v\n", i, perElf)
2330
sums[i] = sumIntSlice(perElf)
2431
}
25-
return maxIntSlice(sums)
32+
33+
for i := 0; i < topN; i++ {
34+
// fmt.Printf("%d %v %v\n", i, maxes, sums)
35+
max, maxI := maxIntSlice(sums)
36+
maxes[i] = max
37+
sums[maxI] = 0
38+
}
39+
40+
// return maxIntSlice(sums)
41+
return maxes
2642
}
2743

2844
func sumIntSlice(ints []int) (m int) {
@@ -33,13 +49,15 @@ func sumIntSlice(ints []int) (m int) {
3349
return sum
3450
}
3551

36-
func maxIntSlice(v []int) (m int) {
52+
func maxIntSlice(v []int) (m int, maxI int) {
3753
if len(v) > 0 {
3854
m = v[0]
55+
maxI = 0
3956
}
4057
for i := 1; i < len(v); i++ {
4158
if v[i] > m {
4259
m = v[i]
60+
maxI = i
4361
}
4462
}
4563
return
@@ -78,4 +96,7 @@ func parseInput(input string) [][]int {
7896
func main() {
7997
output := partOne(input2)
8098
fmt.Println(output)
99+
100+
output2 := partTwo(input2)
101+
fmt.Println(output2)
81102
}

‎day01/main_test.go

Copy file name to clipboardExpand all lines: day01/main_test.go
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ func TestOne(t *testing.T) {
1515
assert.Equal(t, 24000, partOne(input1), "they should be equal")
1616
assert.Equal(t, 66616, partOne(input2), "they should be equal")
1717
}
18+
19+
func TestTwo(t *testing.T) {
20+
assert.Equal(t, 45000, partTwo(input1), "they should be equal")
21+
assert.Equal(t, 199172, partTwo(input2), "they should be equal")
22+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.