Created
December 23, 2017 17:23
-
-
Save BeyondEvil/a7df9aef3771afbf16a63af99a8750d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def read_input(): | |
with open('input_test.txt', 'r') as f: | |
return f.read().strip() | |
def parse(pattern): | |
if len(pattern) == 5: | |
r = c = 2 | |
elif len(pattern) == 11: | |
r = c = 3 | |
elif len(pattern) == 19: | |
r = c = 4 | |
else: | |
raise | |
array = np.zeros((r, c), dtype=int) | |
r = c = 0 | |
for char in pattern: | |
if char == '#': | |
array[r, c] = 1 | |
elif char == '/': | |
r += 1 | |
c = -1 | |
c += 1 | |
return array | |
def build_list(rule, enhance, rules): | |
index = 0 | |
flipped = False | |
while index < 4: | |
hash_rule = tuple(rule.flatten()) | |
if hash_rule not in rules.keys(): | |
rules[hash_rule] = enhance | |
rule = np.rot90(rule) | |
index += 1 | |
if index == 4 and not flipped: | |
rule = np.fliplr(rule) | |
index = 0 | |
flipped = True | |
def find_match(pattern, rules): | |
for hash_rule, enhance in rules.items(): | |
if tuple(pattern.flatten()) == hash_rule: | |
return enhance.copy() | |
def run_it(seq): | |
master_pattern = ".#./..#/###" | |
art = parse(master_pattern) | |
size2_rules = dict() | |
size3_rules = dict() | |
for pair in seq.split('\n'): | |
rule, enhance = pair.split(' => ') | |
a_rule = parse(rule) | |
a_enhance = parse(enhance) | |
if a_rule.size % 2 == 0: | |
build_list(a_rule, a_enhance, size2_rules) | |
elif a_rule.size % 3 == 0: | |
build_list(a_rule, a_enhance, size3_rules) | |
else: | |
raise | |
iterations = 1 | |
for i in range(iterations): | |
if art.size % 2 == 0: | |
match = find_match(art, size2_rules) | |
if match is not None: | |
print match | |
elif art.size % 3 == 0: | |
match = find_match(art, size3_rules) | |
if match is not None: | |
print match | |
else: | |
raise | |
exit(0) | |
print len(size2_rules) | |
print len(size3_rules) | |
print art | |
print art.size | |
print art.shape | |
print art.sum() | |
print('Part 1: ', 0) | |
print('Part 2: ', 0) | |
if __name__ == '__main__': | |
run_it(read_input()) # , |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment