<jupyter_start><jupyter_text>Day 13<jupyter_code>infile = './input/day13.txt'

points = []
folds = []
numRows = 0
numCols = 0

with open(infile) as f:
    for line in f:
        if line[0].isdigit():
            pt = tuple(map(int, line.strip().split(',')))
            numRows = max(numRows, pt[0])
            numCols = max(numCols, pt[1])
            points.append(pt)
        elif line[0] == 'f':
            a, b = line.strip().split(' ')[-1].split('=')
            folds.append((a, int(b)))

ar = [[' ']*(1+numCols) for _ in range(numRows+1)]
for i,j in points:
    ar[i][j] = '#'
    
def makeFold(ar, fold):
    numRows = len(ar)
    numCols = len(ar[0])
    xy, num = fold
    if xy == 'x':
        rowsAbove = num
        rowsBelow = numRows - num - 1
        if rowsAbove > rowsBelow:
            arOut = ar[:num]
            for i in range(num):
                i2 = 2*num - i
                if i2 < numRows:
                    for j in range(numCols):
                        if ar[i2][j] == '#':
                            arOut[i][j] = '#'
        else:
            arOut = ar[:num:-1]
            for i in range(rowsBelow):
                '''
                let numRows = 20, num = 4
                rowsAbove = 4, rowsBelow = 15
                i=11, i2 = 0
                i=12, i2 = 1
                i=13, i2 = 2
                i=14, i2 = 3
                i2 = i - (rowsBelow-rowsAbove)
                i2 = i- (numRows - num - 1 - num)
                i2 = i - (numRows -2*num - 1)
                '''
                i2 = i - (rowsBelow - rowsAbove)
                if i2 >= 0:
                    for j in range(numCols):
                        if ar[i2][j] == '#':
                            arOut[i][j] = '#'
    else:
        colsLeft = num
        colsRight = numCols - num - 1
        if colsLeft > colsRight:
            arOut = [row[:num] for row in ar]
            '''
            let numCols = 20, num = 14
            colsLeft = 14, colsRight = 5
            j = 13, j2 = 15
            j = 12, j2 = 16
            j2 = -j + 2*num
            '''
            for i in range(numRows):
                for j in range(num):
                    j2 = -j + 2*num
                    if j2 < numCols:
                        if ar[i][j2] == '#':
                            arOut[i][j] = '#'
        else:
            arOut = [row[:num:-1] for row in ar]
            '''
            let numCols = 20, num = 4
            colsLeft = 4, colsRight = 15
            j = 14, j2 = 3
            j = 13, j2 = 2
            j2 = j - (colsRight-colsLeft) 
            '''
            for i in range(numRows):
                for j in range(colsRight):
                    j2 = j - (colsRight-colsLeft)
                    if j2 >= 0:
                        if ar[i][j2] == '#':
                            arOut[i][j] = '#'
    return arOut

for i,fold in enumerate(folds):
    ar = makeFold(ar, fold)
    if i==0:
        dotCount = 0
        for line in ar:
            for val in line:
                if val == '#': dotCount += 1
        print(f'Part 1: {dotCount}')
    
# for line in ar:
#     print(''.join(line))
        
arTrans = list(zip(*ar))
for line in arTrans:
    print(''.join(line))<jupyter_output><empty_output><jupyter_text>Day 14  Looping<jupyter_code>from collections import Counter

tpl, _, *rules = open('input/day14.txt').read().split('\n')
rules = dict(r.split(' -> ') for r in rules)
pairs = Counter(map(str.__add__, tpl, tpl[1:]))
chars = Counter(tpl)

for _ in range(40):
    for (a, b), c in pairs.copy().items():
        newChar = rules[a+b]
        pairs[a+b] -= c
        pairs[a+newChar] += c
        pairs[newChar+b] += c
        chars[newChar] += c
        
max(chars.values()) - min(chars.values())<jupyter_output><empty_output><jupyter_text>Recursive<jupyter_code>from collections import Counter
from functools import lru_cache

tpl, _, *rules = open('input/day14.txt').read().split('\n')
rules = dict(r.split(' -> ') for r in rules)

@lru_cache(maxsize=None)
def f(a, b, depth=40):
    if depth == 0: return Counter('')
    x = rules[a+b]
    return Counter(x) + f(a, x, depth-1) + f(x, b, depth-1)

c = sum(map(f, tpl, tpl[1:]), Counter(tpl))
max(c.values()) - min(c.values())<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup