diff --git a/17/a.py b/17/a.py new file mode 100644 index 0000000..205f786 --- /dev/null +++ b/17/a.py @@ -0,0 +1,8 @@ + +import itertools +from copy import deepcopy + +ic = [ list(x) for x in open('input').read().splitlines() ] + +[print(x) for x in ic] + diff --git a/18/a.py b/18/a.py new file mode 100644 index 0000000..d882f1a --- /dev/null +++ b/18/a.py @@ -0,0 +1,48 @@ + +import ast +import math + +exprs = open('input').read().splitlines() + +def specialeval(expr): + expr_arr = ('+ '+expr).replace('(', '( + ').replace(')', ' ) ').split(' ') + stack = [0] + opstack = [] + level = 0 + # print(expr_arr) + for i in range(0,len(expr_arr)-1,2): + # print(stack) + # print([expr_arr[i], expr_arr[i+1]]) + op = expr_arr[i] + if expr_arr[i+1] == '(': + level += 1 + stack.append(0) + opstack.append(expr_arr[i]) + else: + # print(expr_arr[i+1]) + if op == '+': + stack[level] += int(expr_arr[i+1]) + elif op == '*': + stack[level] *= int(expr_arr[i+1]) + else: # ) + op = opstack[level-1] + if op == '+': + stack[level-1] += int(stack[level]) + elif op == '*': + stack[level-1] *= int(stack[level]) + level -= 1 + stack.pop() + opstack.pop() + return stack[0] + +def a(exprs): + return sum([specialeval(expr) for expr in exprs]) + +def b(exprs): + return sum([ + specialeval('('+(expr+' * 1').replace('(','((').replace(')','))').replace(' * ', ') * (')+')') + for expr in exprs + ]) + +print(a(exprs)) +print(b(exprs)) \ No newline at end of file diff --git a/19/a.py b/19/a.py new file mode 100644 index 0000000..e83ee0b --- /dev/null +++ b/19/a.py @@ -0,0 +1,32 @@ + +import re + +# Fook the other solution attempt (a.py.OLD): +# I'm using grep -P now. - getting lazy. + +rules = { rule[0]:'( '+rule[1].replace('"','')+' )' for x in open('input_rules').read().splitlines() if (rule := x.split(': ')) } +rules.update({i:rules[i].replace('( ','').replace(' )','') for i in rules if (rules[i] in ['( a )','( b )'])}) +# print(rules) +def expand(rules,breaker): #You know the rules, and so do I + mod = False + for i in rules: + rule = rules[i].split(' ') + # print(rule) + j = 0 + for k in rule: + if k.isnumeric(): + mod = True + rule[j] = rules[k] + j += 1 + rules[i] = ''.join(rule) + if mod and breaker < 4: + breaker += 1 + rules = expand(rules, breaker) + return rules +# print("grep -P '"+expand(rules)['0']+"' input") +print() +regxp = re.compile('^'+expand(rules,0)['0']+'$') +matched = [ x for x in open('input').read().splitlines() if (regxp.match(x))] +# [print(x)for x in matched] +print(len(matched)) + diff --git a/19/a.py.OLD b/19/a.py.OLD new file mode 100644 index 0000000..2728c0c --- /dev/null +++ b/19/a.py.OLD @@ -0,0 +1,52 @@ +from copy import deepcopy +import itertools + +def unpack(table): + replacable = [] + replacable_idx = [] + for i in range(len(table)): + if type(table[i]) == type([]): + replacable.append(list(itertools.product(*table[i]))) + replacable_idx.append(i) + + solutions = [] + for x in list(itertools.product(*replacable)): + t_ = deepcopy(table) + i = 0 + for y in x: + t_[replacable_idx[i]] = list(y) + i += 1 + solutions.append(list(itertools.chain(*t_))) + return solutions + +def unpackrules(rules): #You know the rules, and so do I + for rule_n in rules: + rule = rules[rule_n] + for j in range(len(rule)): + subrule = rule[j] + for i in range(len(subrule)): + if subrule[i] in list(ab.keys()): + subrule[i] = ab.get(subrule[i]) + else: + subrule[i] = rules[subrule[i]] + rule[j] = subrule + rules[rule_n] = unpack(rule) + return rules + +rules = { y[0]:[ z.split(' ') for z in y[1].split(' | ') ] for x in open('input_rules').read().splitlines() if (y := x.split(': ')) } +ab = { x:rules[x][0][0].replace('"','') for x in rules if (rules[x][0][0] == '"a"' or rules[x][0][0] == '"b"')} +[ rules.pop(x) for x in ab ] + +print(unpackrules(rules)) + + + +# print(unpack(['a', [['2', '3', '76'], ['3', '2']], 'b', [['2', '3'], ['3', '2']], [['2', '3'], ['3', '2']]])) +# print(list(itertools.permutations(['a', [['2', '3', '76'], ['3', '2']], 'b', [['2', '3'], ['3', '2']], [['2', '3'], ['3', '2']]]))) + + +# rules = { x[0]:x[1] for x in [ rule.split(': ') for rule in open('input_rules').read().splitlines() ]} +# ab = { x:rules[x].replace('"','') for x in rules if (rules[x] in ['"a"', '"b"'])} +# rules.update(ab) +# print(rules) +# print(ab) \ No newline at end of file diff --git a/19/whiteboard.png b/19/whiteboard.png new file mode 100644 index 0000000..10071f6 Binary files /dev/null and b/19/whiteboard.png differ