-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7-graph.py
62 lines (48 loc) · 1.86 KB
/
day7-graph.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import re
import networkx as nx
import numpy as np
from aocd.models import Puzzle
pattern = re.compile(r'((\d)?\s?(\w*\s\w+)\s(bags?))')
def solve_puzzle_one(graph):
print(len(nx.ancestors(graph, 'shiny gold')))
def solve_puzzle_two(graph):
print(count_bags(graph, 'shiny gold'))
def count_bags(graph, node):
sum = 0
for successor in graph.successors(node):
weight = graph[node][successor]['weight']
sum += count_bags(graph, successor) * weight + weight
return sum
def parse_input(data):
g = nx.DiGraph()
for rule in np.array(data.splitlines()):
matches = pattern.findall(rule)
for i in range(1, len(matches)):
g.add_edge(matches[0][2], matches[i][2], weight=int(matches[i][1]) if matches[i][1] else 0)
return g
test_input = """light red bags contain 1 bright white bag, 2 muted yellow bags.
faded blue bags contain no other bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
dotted black bags contain no other bags.
"""
test_input2 = """shiny gold bags contain 2 dark red bags.
dark red bags contain 2 dark orange bags.
dark orange bags contain 2 dark yellow bags.
dark yellow bags contain 2 dark green bags.
dark green bags contain 2 dark blue bags.
dark blue bags contain 2 dark violet bags.
dark violet bags contain no other bags."""
if __name__ == '__main__':
puzzle = Puzzle(year=2020, day=7)
if False:
graph = parse_input(test_input)
else:
graph = parse_input(puzzle.input_data)
# print(array)
solve_puzzle_one(graph)
solve_puzzle_two(graph)