<jupyter_start><jupyter_text>Puzzle 1: Slam shuffle - position of card 2019  deal with new deck; N cut; increment N<jupyter_code>import numpy as np
from copy import deepcopy<jupyter_output><empty_output><jupyter_text>Load input<jupyter_code>with open('./input22.txt', 'r') as file:

    data = file.readlines()
data = [x.split() for x in data]
class card_play(object):
    
    def __init__(self, number_cards):
        self.deck = np.linspace(0, number_cards-1, number_cards)
        
    def new_stack(self):
        self.deck = self.deck[::-1]
    
    def cut(self, N):
        if N > 0:
            temp1 = self.deck[0:N]
            temp2 = self.deck[N:]
            self.deck = np.append(temp2, temp1)
        if N < 0:
            temp1 = self.deck[N:]
            temp2 = self.deck[0:N]
            self.deck = np.append(temp1, temp2)
            
    def increment(self, N):
        temp1 = deepcopy(self.deck)
        temp2 = np.zeros(temp1.shape)
        i = 0
        while len(temp1) > 0:
            temp2[i%len(temp2)] = temp1[0]
            temp1 = np.delete(temp1, 0)
            i += N
        self.deck = deepcopy(temp2)
        
    def card_position(self, N):
        return np.where(self.deck == N)[0][0]
deck = card_play(10007)
for operation in data:
    if operation[-2] == 'new':
        deck.new_stack()
    if operation[-2] == 'cut':
        deck.cut(int(operation[-1]))
    if operation[-2] == 'increment':
        deck.increment(int(operation[-1]))

print('Card 2019 is at position {0}.'.format(deck.card_position(2019)))<jupyter_output>Card 2019 is at position 6978.
StarcoderdataJupyterStructuredCleanDedup