re-organise repo
This commit is contained in:
44
2022/go/day01/day01.go
Normal file
44
2022/go/day01/day01.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package day01
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"adventofcode2022/utils"
|
||||
)
|
||||
|
||||
type elf struct {
|
||||
calories int
|
||||
}
|
||||
|
||||
func Part1(input string) int {
|
||||
elves := common(input)
|
||||
return elves[0].calories
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
elves := common(input)
|
||||
return elves[0].calories + elves[1].calories + elves[2].calories
|
||||
}
|
||||
|
||||
func common(input string) []elf {
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
elves := []elf{}
|
||||
e := elf{}
|
||||
for _, line := range lines {
|
||||
if line == "" {
|
||||
elves = append(elves, e)
|
||||
e = elf{}
|
||||
} else {
|
||||
e.calories += utils.MustAtoi(line)
|
||||
}
|
||||
}
|
||||
elves = append(elves, e)
|
||||
|
||||
sort.Slice(elves, func(i, j int) bool {
|
||||
return elves[i].calories > elves[j].calories
|
||||
})
|
||||
|
||||
return elves
|
||||
}
|
||||
43
2022/go/day01/day01_test.go
Normal file
43
2022/go/day01/day01_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package day01
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(`1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000`)
|
||||
assert.Equal(t, 24000, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(`1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000`)
|
||||
assert.Equal(t, 45000, r)
|
||||
}
|
||||
2255
2022/go/day01/input.txt
Normal file
2255
2022/go/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user