StyleInCode

RSS

 

Day 24 - Advent of Code 2024

24 December 2024

Working solutions for the day 24 puzzles.

Part One

""" day_24_01.py """

# usage: python3 day_24_01.py < input

import sys


variables = {}
functions = {}
with sys.stdin as infile:
    while (assignment := input()) != '':
        wire, value = assignment.split(': ')
        variables[wire] = int(value)
    for connection in infile:
        c = connection.split()
        functions[c[4]] = c[:3]

while functions:
    solved = []
    for v, (x, op, y) in functions.items():
        if x in variables and y in variables:
            match op.lower():
                case 'and':
                    variables[v] = variables[x] & variables[y]
                case 'or':
                    variables[v] = variables[x] | variables[y]
                case 'xor':
                    variables[v] = variables[x] ^ variables[y]
            solved.append(v)
    for v in solved:
        del functions[v]

z = sorted([(i, j) for i, j in variables.items() if i.startswith('z')],
           reverse=True)

print(int(''.join([str(v) for _, v in z]), base=2))

Part Two


Categories

Links