Skip to content

Commit

Permalink
Use bitwise operations in 2024 day 22
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 23, 2024
1 parent 9c202dd commit 1580750
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/scala/eu/sim642/adventofcode2024/Day22.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ object Day22 {
type Secret = Long

def mix(secret: Secret, value: Secret): Secret = value ^ secret
def prune(secret: Secret): Secret = secret % 16777216 // TODO: bitwise
def prune(secret: Secret): Secret = secret & 0xFFFFFF // % 16777216

def nextSecret(secret: Secret): Secret = {
val secret1 = prune(mix(secret, secret * 64))
val secret2 = prune(mix(secret1, secret1 / 32))
prune(mix(secret2, secret2 * 2048))
val secret1 = prune(mix(secret, secret << 6)) // * 64
val secret2 = mix(secret1, secret1 >> 5) // / 32, no prune needed after right shift
prune(mix(secret2, secret2 << 11)) // * 2048
}

def secretIterator(initialSecret: Secret): Iterator[Secret] =
Expand Down

0 comments on commit 1580750

Please sign in to comment.