elif instr[0] == 'F': return step((directions[facing], instr[1]), loc, facing) else: return movementFuncs[instr[0]](loc, instr[1]), facing def step2(instr, shipLoc, waypointLoc): if instr[0] == 'L' or instr[0] == 'R': return shipLoc, rotate(instr, waypointLoc) elif instr[0] == 'F': return moveToWaypoint(instr[1], shipLoc, waypointLoc), waypointLoc else: return shipLoc, movementFuncs[instr[0]](waypointLoc, instr[1]) input = getLinesFromFile('input.txt') movementFuncs = { 'E': lambda coord, dist: (coord[0] + dist, coord[1]), 'S': lambda coord, dist: (coord[0], coord[1] - dist), 'W': lambda coord, dist: (coord[0] - dist, coord[1]), 'N': lambda coord, dist: (coord[0], coord[1] + dist), } directions = ['E', 'S', 'W', 'N'] facingIndex = 0 currentLocation = (0, 0) instructions = [(line[0], int(line[1:])) for line in input] #part1 for instr in instructions: currentLocation, facingIndex = step(instr, currentLocation, facingIndex)
def countVisibleOccupied(row, col, state): mask = [ lambda r,c: (r-1,c-1), lambda r,c: (r-1,c), lambda r,c: (r-1,c+1), lambda r,c: (r,c-1), lambda r,c: (r,c+1), lambda r,c: (r+1,c-1), lambda r,c: (r+1,c), lambda r,c: (r+1,c+1), ] count = 0 for los in mask: if canSeeOccupied(row,col,state, los): count += 1 return count def countOccupied(state): return sum([len([seat for seat in row if seat == OCCUPIED]) for row in state]) input = [list(line) for line in getLinesFromFile('input.txt')] currentState = input changedSeats = -1 while changedSeats != 0: changedSeats = 0 nextState = deepcopy(currentState) for row in range(len(currentState)): for seat in range(len(currentState[0])): if currentState[row][seat] == VACANT and countAdjacentOccupied(row,seat, currentState) == 0: #empty and no one sitting adjacent nextState[row][seat] = OCCUPIED changedSeats += 1 elif currentState[row][seat] == OCCUPIED and countAdjacentOccupied(row,seat, currentState) >= 4: #occupied and 4 or more people in adjacent seats nextState[row][seat] = VACANT changedSeats += 1
def isValidForPreamble(preamble, value): candidateValues = set([sum(x) for x in combinations(preamble,2)]) return value in candidateValues def findEncryptionWeakness(pivot, targetValue): sizes = range(3,pivot) for windowLength in sizes: contiguousStart = 0 while(contiguousStart + windowLength < pivot): window = input[contiguousStart:contiguousStart+windowLength] if sum(window) == targetValue: return min(window) + max(window) contiguousStart += 1 input = [int(x) for x in getLinesFromFile('input.txt')] preambleStart = 0 preambleLength = 25 while(isValidForPreamble(input[preambleStart:preambleStart+preambleLength], input[preambleStart + preambleLength])): preambleStart += 1 #part1 answer print(preambleStart + preambleLength) invalid = input[preambleStart + preambleLength] contiguousStart = 0 contiguousLength = 2 while(sum(input[contiguousStart:contiguousStart+contiguousLength]) <= invalid): contiguousStart += 1
splitAddress[1:], ''.join([currentAddress, splitAddress[0], '0'])) yield from floatingAddressGenerator( splitAddress[1:], ''.join([currentAddress, splitAddress[0], '1'])) def getFloatingAddress(address, mask): binAddress = bin(address)[2:].zfill(36) for m in re.finditer('1', mask): binAddress = binAddress[:m.start()] + '1' + binAddress[m.start() + 1:] for m in re.finditer('X', mask): binAddress = binAddress[:m.start()] + 'X' + binAddress[m.start() + 1:] return binAddress mem = defaultdict(int) input = [line.rstrip('\n') for line in getLinesFromFile('input.txt')] for line in input: if line.startswith('mask'): onMask = int(line[-36:].replace('X', '0'), 2) offMask = int(line[-36:].replace('X', '1'), 2) else: m = re.match(r'mem\[(\d+)\] = (\d+)', line) mem[m[1]] = (int(m[2]) | onMask) & offMask #part1 print(sum(mem.values())) mem = defaultdict(int) for line in input: if line.startswith('mask'):