2020/14.py
def part1():
    """
    Part 1, I did some bit-hacking instead of doing it stringly typed which was a
    mistake as it screwed me for part 2, and it was also way more difficult than it was
    worth.

    I also need to be better at Python's built-in integer conversion libraries.
    """
    # Using a dict to store memory because I don't want to figure out much memory to
    # pre-allocate.
    mem = {}

    # andmask and ormask start out as the identity for each of the corresponding
    # operations.
    # For any given bit, X & 1 = X
    # For any given bit, X | 0 = X
    andmask = 1
    ormask = 0
    for line in lines:
        if rematch("mask.*", line):
            # Updating the masks.
            andmask = 1
            ormask = 0

            # Go through all of the characters in the mask.
            for x in rematch("mask = (.*)", line).group(1):
                # First, binary left shift the andmask and ormask by one. This moves all
                # the bits over to the left by 1, leaving a 0 at the least-significant
                # bit (LSB).
                # This is equivalent to multiplying both of these by 2.
                andmask = andmask << 1
                ormask = ormask << 1
                if x != "0":
                    # Either X or 1, we need to preserve the value, so set the andmask
                    # LSB to 1, which is the identity. If it is 0, then we let it stay
                    # because we want it to override the existing value.
                    andmask |= 1
                if x == "1":
                    # If it's a 1, then set the ormask to 1, so that it overrides it. In
                    # all other casse, we just leave the ormask as is since it is
                    # already the identity.
                    ormask |= 1
        else:
            # Destructure the memory location and apply the andmask and ormask to the
            # value before setting it.
            loc, val = map(int, rematch(r"mem\[(\d+)\] = (\d+)", line).groups())
            mem[loc] = val & andmask | ormask

    return sum(mem.values())

    Part 1, I did some bit-hacking instead of doing it stringly typed which was a
    mistake as it screwed me for part 2, and it was also way more difficult than it was
    worth.

    I also need to be better at Python's built-in integer conversion libraries.
    
Part 1, I did some bit-hacking instead of doing it stringly typed which was a
mistake as it screwed me for part 2, and it was also way more difficult than it was
worth.

I also need to be better at Python's built-in integer conversion libraries.
Part 1 I did some bit - hacking instead of doing it stringly typed which was a mistake as it screwed me for part 2 and it was also way more difficult than it was worth . I also need to be better at Python ' s built - in integer conversion libraries .
def part1():
    mem = {}
    andmask = 1
    ormask = 0
    for line in lines:
        if rematch("mask.*", line):
            andmask = 1
            ormask = 0
            for x in rematch("mask = (.*)", line).group(1):
                andmask = andmask << 1
                ormask = ormask << 1
                if x != "0":
                    andmask |= 1
                if x == "1":
                    ormask |= 1
        else:
            loc, val = map(int, rematch(r"mem\[(\d+)\] = (\d+)", line).groups())
            mem[loc] = val & andmask | ormask
    return sum(mem.values())
def part1 ( ) : mem = { } andmask = 1 ormask = 0 for line in lines : if rematch ( "mask.*" , line ) : andmask = 1 ormask = 0 for x in rematch ( "mask = (.*)" , line ) . group ( 1 ) : andmask = andmask << 1 ormask = ormask << 1 if x != "0" : andmask |= 1 if x == "1" : ormask |= 1 else : loc , val = map ( int , rematch ( r"mem\[(\d+)\] = (\d+)" , line ) . groups ( ) ) mem [ loc ] = val & andmask | ormask return sum ( mem . values ( ) )
Part 1, I did some bit-hacking instead of doing it stringly typed which was a
mistake as it screwed me for part 2, and it was also way more difficult than it was
worth.
Part 1 I did some bit - hacking instead of doing it stringly typed which was a mistake as it screwed me for part 2 and it was also way more difficult than it was worth .
"""
    Part 1, I did some bit-hacking instead of doing it stringly typed which was a
    mistake as it screwed me for part 2, and it was also way more difficult than it was
    worth.

    I also need to be better at Python's built-in integer conversion libraries.
    """ # Using a dict to store memory because I don't want to figure out much memory to # pre-allocate. # andmask and ormask start out as the identity for each of the corresponding # operations. # For any given bit, X & 1 = X # For any given bit, X | 0 = X # Updating the masks. # Go through all of the characters in the mask. # First, binary left shift the andmask and ormask by one. This moves all # the bits over to the left by 1, leaving a 0 at the least-significant # bit (LSB). # This is equivalent to multiplying both of these by 2. # Either X or 1, we need to preserve the value, so set the andmask # LSB to 1, which is the identity. If it is 0, then we let it stay # because we want it to override the existing value. # If it's a 1, then set the ormask to 1, so that it overrides it. In # all other casse, we just leave the ormask as is since it is # already the identity. # Destructure the memory location and apply the andmask and ormask to the # value before setting it.