-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.py
113 lines (97 loc) · 3.58 KB
/
19.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from collections import defaultdict
import re
lines = [x.strip() for x in open('19.test', 'r').readlines() if x != '']
lines = [x.strip() for x in open('19.test2', 'r').readlines() if x != '']
lines = [x.strip() for x in open('19.input', 'r').readlines() if x != '']
stop = lines.index('')
rules = dict([r.split(': ') for r in lines[:stop]])
messages = lines[stop+1:]
def part1(rules, messages):
def valid1(rule_key, msg, index):
rule = rules[rule_key]
if index >= len(msg):
return []
if rule == '"a"':
return [index + 1] if msg[index] == 'a' else [index]
elif rule == '"b"':
return [index + 1] if msg[index] == 'b' else [index]
valid_combinations = []
for pipe in rule.split(' | '):
rule_order = pipe.split(' ')
combination_list = valid1(rule_order[0], msg, index)
for current_rule in rule_order[1:]:
if len(combination_list) == 0:
break
tmp = []
for new_index in combination_list:
if new_index != index:
tmp += valid1(current_rule, msg, new_index)
combination_list = tmp
valid_combinations += set(list(combination_list))
return valid_combinations
valid_count = 0
for i, msg in enumerate(messages):
mem = {}
v = valid1('0', msg, 0)
is_valid = len(msg) in v
#print i+1, "Is valid:", is_valid, msg
if is_valid:
valid_count += 1
print "Part 1 solution: %d" % valid_count
part1(rules, messages)
# ===================================
# ===================================
# ===================================
def divide_in_half(str):
halfpoint = (len(str)/2)
return str[:halfpoint], str[halfpoint:]
def chunk_str(s, n):
return [s[i:i+n] for i in range(0, len(s), n)]
def part2(rules, messages):
def possible_combinations(rule_key, rules):
rule = rules[rule_key]
if rule in '"a"':
return ['a']
elif rule == '"b"':
return ['b']
valid_combinations = []
for pipe in rule.split(' | '):
rule_order = pipe.split(' ')
combination_list = possible_combinations(rule_order[0], rules)
for current_rule in rule_order[1:]:
tmp = []
for comb in combination_list:
new_combs = possible_combinations(current_rule, rules)
for nc in new_combs:
tmp.append(comb + nc)
combination_list = list(set(tmp))
valid_combinations += combination_list
return valid_combinations
def valid(msg, a, b, n):
head = msg[:n]
tail = msg[n+0:]
if head not in a:
return False
if len(tail) <= n:
return False
tail = chunk_str(tail, n)
left, right = divide_in_half(tail)
is_valid = (
len(left) == len(right) and
all([(x in a) for x in left]) and
all([(x in b) for x in right])
)
if is_valid:
return True
return valid(msg[n:], a,b,n)
# Deferred rules from input
a = possible_combinations('42', rules)
b = possible_combinations('31', rules)
valid_count = 0
for i, m in enumerate(messages):
is_valid = valid(m, a, b, len(a[0]))
#print "Validating (%d):" % i, is_valid, valid_count, m
if is_valid:
valid_count += 1
print "Part 2 solution: %d" % valid_count
part2(rules, messages)