Skip to content

Commit

Permalink
day22p1
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 22, 2024
1 parent 391a4db commit ab208bd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
40 changes: 40 additions & 0 deletions day22p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package day22p1

import (
"io"
"strconv"

"aoc/utils"
)

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

sum := 0
for _, line := range lines {
initialSecretNum, _ := strconv.Atoi(line)
newSecretNum := initialSecretNum
for i := 0; i < 2000; i++ {
newSecretNum = evolveSecretNum(newSecretNum)
}
sum += newSecretNum
}

return sum
}

func evolveSecretNum(initialSecretNum int) int {
secretNum := initialSecretNum
secretNum = prune(mix(secretNum, secretNum*64))
secretNum = prune(mix(secretNum, secretNum/32))
secretNum = prune(mix(secretNum, secretNum*2048))
return secretNum
}

func mix(a, b int) int {
return a ^ b
}

func prune(a int) int { // modulo 16777216 (2^24)
return a & 0xFFFFFF
}
36 changes: 36 additions & 0 deletions day22p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package day22p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `1
10
100
2024`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 37327623},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}

0 comments on commit ab208bd

Please sign in to comment.