Compare commits

..

2 Commits

Author SHA1 Message Date
b685e81c58 feat: solve part two 2025-12-12 18:35:11 +01:00
1adc10ea88 test: add test for part two 2025-12-12 18:35:05 +01:00
2 changed files with 56 additions and 4 deletions

View File

@@ -19,15 +19,16 @@ func ParseInput(filepath string) string {
func PartOne(data string) int { func PartOne(data string) int {
doorIDBytes := []byte(data) doorIDBytes := []byte(data)
doorIDLen := len(doorIDBytes)
input := make([]byte, doorIDLen, doorIDLen+20)
copy(input, doorIDBytes)
password := make([]byte, 0, 8) password := make([]byte, 0, 8)
index := 0 index := 0
hexChars := "0123456789abcdef" hexChars := "0123456789abcdef"
var buffer []byte
for len(password) < 8 { for len(password) < 8 {
buffer = strconv.AppendInt(buffer[:0], int64(index), 10) indexBytes := strconv.AppendInt(input[:doorIDLen], int64(index), 10)
input := append(doorIDBytes, buffer...) hash := md5.Sum(indexBytes)
hash := md5.Sum(input)
if hash[0] == 0 && hash[1] == 0 && hash[2] < 16 { if hash[0] == 0 && hash[1] == 0 && hash[2] < 16 {
char := hexChars[hash[2]&0x0F] char := hexChars[hash[2]&0x0F]
@@ -41,5 +42,32 @@ func PartOne(data string) int {
} }
func PartTwo(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 return 0
} }

View File

@@ -32,3 +32,27 @@ func TestPartOne(t *testing.T) {
t.Errorf("PartOne() printed %q, want %q", got, expected) t.Errorf("PartOne() printed %q, want %q", got, expected)
} }
} }
func TestPartTwo(t *testing.T) {
expected := "05ace8e3"
oldStdout := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe: %v", err)
}
os.Stdout = w
PartTwo(testInput)
_ = w.Close()
os.Stdout = oldStdout
var buffer bytes.Buffer
_, _ = buffer.ReadFrom(r)
got := strings.TrimSpace(buffer.String())
if got != expected {
t.Errorf("PartTwo() printed %q, want %q", got, expected)
}
}