Now using regex for data parsing

This commit is contained in:
Daniel Cortés
2020-12-07 07:31:00 -03:00
parent 675f82e72d
commit 6659174e41

View File

@@ -1,14 +1,14 @@
import re
def process_rules(data):
rules = {}
for line in data:
line = line.replace(' bags', '').replace(' bag', '').replace('.', '')
color, rule = [r.strip() for r in line.split('contain ')]
rule = rule.split(', ')
color = re.search("(\w+ \w+) bags contain", line).group(1)
rule = re.findall("(\d+) (\w+ \w+)", line)
rules[color] = []
for bag in rule:
if bag == 'no other': break
rules[color].append((bag[2:], int(bag[0])))
rules[color].append((bag[1], int(bag[0])))
return rules