Compare commits
4 Commits
2f06c7ab2d
...
8915de6145
| Author | SHA1 | Date | |
|---|---|---|---|
| 8915de6145 | |||
| e96d308e5f | |||
| 3fdb921aae | |||
| 2d5e05ae8b |
@@ -16,6 +16,7 @@ import (
|
|||||||
_ "advent-of-code/internal/2021/DayOne"
|
_ "advent-of-code/internal/2021/DayOne"
|
||||||
_ "advent-of-code/internal/2021/DayThree"
|
_ "advent-of-code/internal/2021/DayThree"
|
||||||
_ "advent-of-code/internal/2021/DayTwo"
|
_ "advent-of-code/internal/2021/DayTwo"
|
||||||
|
_ "advent-of-code/internal/2022/DayOne"
|
||||||
)
|
)
|
||||||
|
|
||||||
func capitalize(day string) string {
|
func capitalize(day string) string {
|
||||||
|
|||||||
62
internal/2022/DayOne/code.go
Normal file
62
internal/2022/DayOne/code.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package dayone
|
||||||
|
|
||||||
|
import (
|
||||||
|
"advent-of-code/internal/registry"
|
||||||
|
"cmp"
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registry.Register("2022D1", ParseInput, PartOne, PartTwo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseInput(filepath string) []string {
|
||||||
|
content, _ := os.ReadFile(filepath)
|
||||||
|
return strings.Split(string(content), "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func PartOne(input []string) int {
|
||||||
|
maxCalories, currentElf := 0, 0
|
||||||
|
|
||||||
|
for _, line := range input {
|
||||||
|
if line == "" {
|
||||||
|
maxCalories = max(maxCalories, currentElf)
|
||||||
|
currentElf = 0
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val, _ := strconv.Atoi(line)
|
||||||
|
currentElf += val
|
||||||
|
}
|
||||||
|
|
||||||
|
return max(maxCalories, currentElf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PartTwo(input []string) int {
|
||||||
|
var totals []int
|
||||||
|
currentElf := 0
|
||||||
|
|
||||||
|
for _, line := range input {
|
||||||
|
if line == "" {
|
||||||
|
totals = append(totals, currentElf)
|
||||||
|
currentElf = 0
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val, _ := strconv.Atoi(line)
|
||||||
|
currentElf += val
|
||||||
|
}
|
||||||
|
totals = append(totals, currentElf)
|
||||||
|
|
||||||
|
slices.SortFunc(totals, func(a, b int) int {
|
||||||
|
return cmp.Compare(b, a)
|
||||||
|
})
|
||||||
|
|
||||||
|
sum := 0
|
||||||
|
for idx := 0; idx < 3 && idx < len(totals); idx++ {
|
||||||
|
sum += totals[idx]
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
36
internal/2022/DayOne/code_test.go
Normal file
36
internal/2022/DayOne/code_test.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package dayone
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
var testInput = []string{
|
||||||
|
"1000",
|
||||||
|
"2000",
|
||||||
|
"3000",
|
||||||
|
"",
|
||||||
|
"4000",
|
||||||
|
"",
|
||||||
|
"5000",
|
||||||
|
"6000",
|
||||||
|
"",
|
||||||
|
"7000",
|
||||||
|
"8000",
|
||||||
|
"9000",
|
||||||
|
"",
|
||||||
|
"10000",
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPartOne(t *testing.T) {
|
||||||
|
expected := 24000
|
||||||
|
got := PartOne(testInput)
|
||||||
|
if got != expected {
|
||||||
|
t.Errorf("PartOne() = %d, want %d", got, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPartTwo(t *testing.T) {
|
||||||
|
expected := 45000
|
||||||
|
got := PartTwo(testInput)
|
||||||
|
if got != expected {
|
||||||
|
t.Errorf("PartTwo() = %d, want %d", got, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
2234
internal/data/2022/DayOne/input.txt
Normal file
2234
internal/data/2022/DayOne/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user