-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.py
More file actions
34 lines (22 loc) · 855 Bytes
/
Copy pathcommon.py
File metadata and controls
34 lines (22 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import re
from typing import Dict
rule_regex = re.compile(r"(.+) bags contain (.+).")
definition_regex = re.compile(
r"(\d+) (.+) bags?"
) # A definiton is the section of a rule that contains the conditions
target_colour = "shiny gold"
def parse(instr: str) -> Dict[str, Dict[str, int]]:
inp = instr.strip().split("\n")
rules = {}
for rule in inp:
rr = rule_regex.match(rule)
container_bag = rr.group(1)
rule_set = rr.group(2).split(", ")
bag_rules = {}
for definition in rule_set:
rsr = definition_regex.match(definition)
# if this is false, it probably means we've encountered something saying "no other bags"
if rsr is not None:
bag_rules[rsr.group(2)] = int(rsr.group(1))
rules[container_bag] = bag_rules
return rules