From a0ce63e5a514e14e510c4739b4a894ade995767c Mon Sep 17 00:00:00 2001 From: Kharec Date: Sat, 29 Nov 2025 09:57:54 +0100 Subject: [PATCH] feat: solve d4 both parts using md5 bruteforce --- internal/2015/DayFour/code.go | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 internal/2015/DayFour/code.go diff --git a/internal/2015/DayFour/code.go b/internal/2015/DayFour/code.go new file mode 100644 index 0000000..32aee52 --- /dev/null +++ b/internal/2015/DayFour/code.go @@ -0,0 +1,46 @@ +package dayfour + +import ( + "advent-of-code/internal/registry" + "bytes" + "crypto/md5" + "os" + "strconv" +) + +func init() { + registry.Register("2015D4", ParseInput, PartOne, PartTwo) +} + +func ParseInput(filepath string) []byte { + content, _ := os.ReadFile(filepath) + return bytes.TrimSpace(content) +} + +func findHashWithZeroes(data []byte, zeroes int) int { + buffer := make([]byte, len(data), len(data)+10) + copy(buffer, data) + for idx := 1; ; idx++ { + buffer = buffer[:len(data)] + buffer = strconv.AppendInt(buffer, int64(idx), 10) + hash := md5.Sum(buffer) + switch zeroes { + case 5: + if hash[0] == 0 && hash[1] == 0 && hash[2]&0xF0 == 0 { + return idx + } + case 6: + if hash[0] == 0 && hash[1] == 0 && hash[2] == 0 { + return idx + } + } + } +} + +func PartOne(data []byte) int { + return findHashWithZeroes(data, 5) +} + +func PartTwo(data []byte) int { + return findHashWithZeroes(data, 6) +}