From 98fb052039127b7936ff6f96f83dfb5b4cfbaa1e Mon Sep 17 00:00:00 2001 From: Kharec Date: Fri, 28 Nov 2025 15:33:23 +0100 Subject: [PATCH] feat: add p2 solution --- internal/2020/DayEight/code.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/internal/2020/DayEight/code.go b/internal/2020/DayEight/code.go index 6d01bff..a0f3b96 100644 --- a/internal/2020/DayEight/code.go +++ b/internal/2020/DayEight/code.go @@ -16,11 +16,6 @@ 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 { @@ -61,6 +56,11 @@ func execute(instructions []instruction) (accumulator int, terminatedNormally bo return accumulator, true } +func ParseInput(filepath string) []string { + content, _ := os.ReadFile(filepath) + return strings.Split(string(content), "\n") +} + func PartOne(data []string) int { instructions := parseInstructions(data) accumulator, _ := execute(instructions) @@ -68,5 +68,27 @@ func PartOne(data []string) int { } func PartTwo(data []string) int { + instructions := parseInstructions(data) + + for idx := range instructions { + if instructions[idx].operation != "jmp" && instructions[idx].operation != "nop" { + continue + } + + originalOperation := instructions[idx].operation + if originalOperation == "jmp" { + instructions[idx].operation = "nop" + } else { + instructions[idx].operation = "jmp" + } + + accumulator, terminatedNormally := execute(instructions) + instructions[idx].operation = originalOperation + + if terminatedNormally { + return accumulator + } + } + return 0 }