Compare commits

...

4 Commits

Author SHA1 Message Date
8915de6145 feat: add 2022D1 solutions 2025-11-26 16:05:47 +01:00
e96d308e5f feat: input for 2022D1 2025-11-26 16:05:35 +01:00
3fdb921aae tests: add 2022D1 unit tests 2025-11-26 16:05:00 +01:00
2d5e05ae8b feat: include 2022D1 2025-11-26 16:04:51 +01:00
4 changed files with 2333 additions and 0 deletions

View File

@@ -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 {

View 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
}

View 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)
}
}

File diff suppressed because it is too large Load Diff