Skip to content

Commit

Permalink
Use functools.cache and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 28, 2024
1 parent bda6551 commit 890c738
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions aoc2024/src/day22/python/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Sequence
from functools import cache


def _mix(secret_number: int, value: int) -> int:
"""Mixes a value and the secret number."""
return secret_number ^ value


def _prune(secret_number: int) -> int:
"""Prunes the secret number."""
return secret_number % 16777216


@cache
def _generate_secret_number(secret_number: int) -> int:
result = secret_number
result = _prune(_mix(result, result * 64))
result = _prune(_mix(result, result // 32))
return _prune(_mix(result, result * 2048))


def generate_secret_number(secret_number: int, times: int = 1) -> int:
result = secret_number
for _ in range(times):
result = _prune(_mix(result, result * 64))
result = _prune(_mix(result, result // 32))
result = _prune(_mix(result, result * 2048))
result = _generate_secret_number(result)
return result


Expand Down

0 comments on commit 890c738

Please sign in to comment.