diff --git a/internal/2020/DayEight/code.go b/internal/2020/DayEight/code.go new file mode 100644 index 0000000..d9261e5 --- /dev/null +++ b/internal/2020/DayEight/code.go @@ -0,0 +1,67 @@ +package dayeight + +import ( + "advent-of-code/internal/registry" + "os" + "strconv" + "strings" +) + +type instruction struct { + operation string + argument int +} + +func init() { + registry.Register("2020D8", ParseInput, PartOne, PartTwo) +} + +func ParseInput(filepath string) []string { + content, _ := os.ReadFile(filepath) + return strings.Split(string(content), "\n") +} + +func parseInstructions(data []string) []instruction { + instructions := make([]instruction, 0, len(data)) + for _, line := range data { + line = strings.TrimSpace(line) + parts := strings.Fields(line) + if len(parts) != 2 { + continue + } + arg, _ := strconv.Atoi(parts[1]) + instructions = append(instructions, instruction{operation: parts[0], argument: arg}) + } + return instructions +} + +func PartOne(data []string) int { + instructions := parseInstructions(data) + visited := make(map[int]bool) + accumulator := 0 + pc := 0 + + for pc < len(instructions) { + if visited[pc] { + return accumulator + } + visited[pc] = true + + instruction := instructions[pc] + switch instruction.operation { + case "acc": + accumulator += instruction.argument + pc++ + case "jmp": + pc += instruction.argument + case "nop": + pc++ + } + } + + return accumulator +} + +func PartTwo(data []string) int { + return 0 +}