StyleInCode

RSS

 

Day 22 - Advent of Code 2024

Working solutions for the day 22 puzzles.

Part One

""" day_22_01.py """

# usage: python3 day_22_01.py < input

import sys


def next_secret(x):
    """ next secret number after x """
    def mix(x, y):
        """ x xor y """
        return x ^ y

    def prune(x):
        """ x % (2 ** 24) """
        return x & ((1 << 24) - 1)

    num = prune(mix(x << 6, x))
    num = prune(mix(num >> 5, num))
    num = prune(mix(num << 11, num))

    return num


def next_secret_post_generation(x, gen):
    """ next secret after gen iterations """
    num = x
    for _ in range(gen):
        num = next_secret(num)
    return num


with sys.stdin as infile:
    print(sum(next_secret_post_generation(int(line), 2000) for line in infile))

Part Two

permalink


Categories

Links