Part 1 day 19

master
E. Almqvist 4 years ago
parent 4fa839bff3
commit 611d87cba7
  1. 118
      2020/19.py

@ -1,8 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
from aoc import get_input # AoC from aoc import get_input # AoC
import regex
import re import re
from functools import lru_cache
data = get_input(19) data = get_input(19)
@ -17,75 +17,89 @@ for i, rule in enumerate(rules): # make a dict for the rules
rulePrim[int(index)] = val.replace('"', "") rulePrim[int(index)] = val.replace('"', "")
def containsPointers(rule): # function to check if a rule contains a pointer #print(rulePrim)
cont = rulePrim[rule]
contains = False @lru_cache
for char in cont: def containsPointers(ruleStr): # function to check if a rule contains a pointer
if(char.isnumeric()): return any(char.isnumeric() for char in ruleStr)
contains = True
break @lru_cache
def getPointersAtPos(string, starti=0):
thing = set(re.findall( r"\d+", string[starti:] ))
return contains return thing
for rule, cont in rulePrim.items(): # replace everything with the chars instead of pointers # @lru_cache
check = containsPointers(rule) # def genRegex(rule, newrule=None, pointers=None):
# #for rule, cont in rulePrim.items(): # replace everything with the chars instead of pointers
# #print("New rec")
# newrule = newrule or rulePrim[rule]
# print("NEW REC")
while(check): # pointers = pointers or getPointersAtPos(newrule)
for i in range(len(rulePrim[rule])):
char = rulePrim[rule][i]
if(char == " "): # for i, pointer in enumerate(pointers):
continue # pointsTo = rulePrim[int(pointer)]
# repl = f"({rulePrim[int(pointer)]})"
if(char.isnumeric()): # #print(f"Checking pointer {i}/{len(pointers)-1}", end="\r")
v = int(char) # newrule = newrule.replace(str(pointer), repl)
pointsTo = rulePrim[v]
repl = f"{rulePrim[v]}"
if(len(pointsTo) > 1): # #print("")
repl = f"({rulePrim[v]})" # check = getPointersAtPos(newrule)
# if(len(check) > 0):
# return genRegex(rule, newrule)
# else:
# return newrule
rulePrim[rule] = rulePrim[rule].replace(char, repl) @lru_cache
def genRegex(rule):
cont = rulePrim[rule]
cont = cont.split(" ")
check = containsPointers(rule) reg = "("
for char in cont:
ispointer = char.isnumeric()
if( ispointer ):
reg += genRegex(int(char))
else:
reg += char
reg += ")"
#rulePrim[rule] = rulePrim[rule].replace(" ", "") return reg
def findParens(s):
toret = dict() def removeSpaces(rule):
pstack = [] # remove spaces because they are evil
cont = rulePrim[rule]
print("########################", cont)
rulePrim[rule] = cont.replace(" ", "")
rulePrim[rule] = "^" + rulePrim[rule] + "$"
for i, c in enumerate(s): return rulePrim[rule]
if( c == "(" ):
pstack.append(i)
elif( c == ")" ):
if( len(pstack) == 0 ):
raise IndexError("No matching closing parens at: " + str(i))
toret[pstack.pop()] = i
if(len(pstack) > 0):
raise IndexError("No matching opening parens at: " + str(pstack.pop()))
return toret # go through all messages and check
def part1(rulePrim, messages):
count = 0
mesLen = len(messages)
for i, message in enumerate(messages):
check = re.match(rulePrim[0], message)
print(f"Checking {message} [{i}/{mesLen}]")
if(check):
print("is valid")
count += 1
def getHighestParen(parens): print("Part1:", count)
bestkey = None
bestdiff = None
for k, v in parens.items():
diff = v - k
if(bestdiff == None):
bestdiff = diff
bestkey = k
continue
if(diff > bestdiff):
bestdiff = diff
bestkey = k
return bestkey rulePrim[8] = "42 | 42 8"
rulePrim[11] = "42 31 | 42 11 31"
rulePrim[0] = genRegex(0)
print(rulePrim[0])
for rule, val in rulePrim.items(): rulePrim[0] = removeSpaces(0)
part1(rulePrim, messages)

Loading…
Cancel
Save