From 1c5bd1e448d5b75928707e20aab5177c7b6f0c39 Mon Sep 17 00:00:00 2001 From: Kharec Date: Sun, 7 Dec 2025 21:55:26 +0100 Subject: [PATCH] feat: solve part one --- internal/2016/DayOne/code.go | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 internal/2016/DayOne/code.go diff --git a/internal/2016/DayOne/code.go b/internal/2016/DayOne/code.go new file mode 100644 index 0000000..7069bf0 --- /dev/null +++ b/internal/2016/DayOne/code.go @@ -0,0 +1,56 @@ +package dayone + +import ( + "os" + "strconv" + "strings" + + "advent-of-code/internal/registry" +) + +func init() { + registry.Register("2016D1", ParseInput, PartOne, PartTwo) +} + +func abs(n int) int { + if n < 0 { + return -n + } + return n +} + +func ParseInput(filepath string) []string { + content, _ := os.ReadFile(filepath) + parts := strings.Split(string(content), ",") + for i, p := range parts { + parts[i] = strings.TrimSpace(p) + } + return parts +} + +func PartOne(data []string) int { + x, y := 0, 0 + directions := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + currentDirection := 0 + + for _, instruction := range data { + turn := instruction[0] + distance, _ := strconv.Atoi(instruction[1:]) + + switch turn { + case 'R': + currentDirection = (currentDirection + 1) % 4 + case 'L': + currentDirection = (currentDirection + 3) % 4 + } + + x += directions[currentDirection][0] * distance + y += directions[currentDirection][1] * distance + } + + return abs(x) + abs(y) +} + +func PartTwo(data []string) int { + return 0 +}