refactor: massive refactor to have only one binary to call

This commit is contained in:
2025-11-26 14:03:32 +01:00
parent 314da54495
commit 3723f84d1a
41 changed files with 272 additions and 188 deletions

View File

@@ -0,0 +1,60 @@
package daytwo
import (
"advent-of-code/internal/registry"
"os"
"strconv"
"strings"
)
func init() {
registry.Register("2021D2", ParseInput, PartOne, PartTwo)
}
func ParseInput(filepath string) []string {
content, _ := os.ReadFile(filepath)
return strings.Split(string(content), "\n")
}
func PartOne(data []string) int {
horizontal := 0
depth := 0
for _, line := range data {
parts := strings.Split(line, " ")
direction := parts[0]
amount, _ := strconv.Atoi(parts[1])
switch direction {
case "forward":
horizontal += amount
case "down":
depth += amount
case "up":
depth -= amount
}
}
return horizontal * depth
}
func PartTwo(data []string) int {
horizontal := 0
depth := 0
aim := 0
for _, line := range data {
parts := strings.Split(line, " ")
direction := parts[0]
amount, _ := strconv.Atoi(parts[1])
switch direction {
case "forward":
horizontal += amount
depth += aim * amount
case "down":
aim += amount
case "up":
aim -= amount
}
}
return horizontal * depth
}

View File

@@ -0,0 +1,29 @@
package daytwo
import "testing"
var testInput = []string{
"forward 5",
"down 5",
"forward 8",
"up 3",
"down 8",
"forward 2",
}
func TestPartOne(t *testing.T) {
expected := 150
got := PartOne(testInput)
if got != expected {
t.Errorf("PartOne() = %d, want %d", got, expected)
}
}
func TestPartTwo(t *testing.T) {
expected := 900
got := PartTwo(testInput)
if got != expected {
t.Errorf("PartTwo() = %d, want %d", got, expected)
}
}