-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |