mirror of
https://github.com/peter-tanner/advent-of-code-2020.git
synced 2024-11-30 10:50:17 +08:00
days 19 and 18 completed! still working on the 3d GOL
This commit is contained in:
parent
7cb0e5bbd1
commit
26f3144fd9
8
17/a.py
Normal file
8
17/a.py
Normal file
|
@ -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]
|
||||
|
48
18/a.py
Normal file
48
18/a.py
Normal file
|
@ -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))
|
32
19/a.py
Normal file
32
19/a.py
Normal file
|
@ -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))
|
||||
|
52
19/a.py.OLD
Normal file
52
19/a.py.OLD
Normal file
|
@ -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)
|
BIN
19/whiteboard.png
Normal file
BIN
19/whiteboard.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 146 KiB |
Loading…
Reference in New Issue
Block a user