From b685e81c585d6c8ef2c817063ac9f21d7b79661a Mon Sep 17 00:00:00 2001 From: Kharec Date: Fri, 12 Dec 2025 18:35:11 +0100 Subject: [PATCH] feat: solve part two --- internal/2016/DayFive/code.go | 36 +++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/internal/2016/DayFive/code.go b/internal/2016/DayFive/code.go index 861a289..154665b 100644 --- a/internal/2016/DayFive/code.go +++ b/internal/2016/DayFive/code.go @@ -19,15 +19,16 @@ func ParseInput(filepath string) string { func PartOne(data string) int { doorIDBytes := []byte(data) + doorIDLen := len(doorIDBytes) + input := make([]byte, doorIDLen, doorIDLen+20) + copy(input, doorIDBytes) password := make([]byte, 0, 8) index := 0 hexChars := "0123456789abcdef" - var buffer []byte for len(password) < 8 { - buffer = strconv.AppendInt(buffer[:0], int64(index), 10) - input := append(doorIDBytes, buffer...) - hash := md5.Sum(input) + indexBytes := strconv.AppendInt(input[:doorIDLen], int64(index), 10) + hash := md5.Sum(indexBytes) if hash[0] == 0 && hash[1] == 0 && hash[2] < 16 { char := hexChars[hash[2]&0x0F] @@ -41,5 +42,32 @@ func PartOne(data string) int { } func PartTwo(data string) int { + doorIDBytes := []byte(data) + doorIDLen := len(doorIDBytes) + input := make([]byte, doorIDLen, doorIDLen+20) + copy(input, doorIDBytes) + password := make([]byte, 8) + filled := make([]bool, 8) + filledCount := 0 + index := 0 + hexChars := "0123456789abcdef" + + for filledCount < 8 { + indexBytes := strconv.AppendInt(input[:doorIDLen], int64(index), 10) + hash := md5.Sum(indexBytes) + + if hash[0] == 0 && hash[1] == 0 && hash[2] < 16 { + position := int(hash[2] & 0x0F) + if position < 8 && !filled[position] { + char := hexChars[hash[3]>>4] + password[position] = char + filled[position] = true + filledCount++ + } + } + index++ + } + + fmt.Println(string(password)) return 0 }