Create a gist now

Instantly share code, notes, and snippets.

@pawlos /advent_of_code_d21.py Secret
Last active Dec 24, 2017

Embed
What would you like to do?
Solution to Day 21: Fractal Art
#aoc_d21.py
rules = open("input_d21.txt").readlines()
'''rules = ["../.# => ##./#../...",
".#./..#/### => #..#/..../..../#..#"]
'''
rules = [r.replace("\n","").split("\\") for r in rules]
d = {}
import re
for r in rules:
#print r
m = re.match("(.*)\s=>\s(.*)",r[0])
if not m:
print "error on rule: ",r
break
d[m.group(1)] = m.group(2)
def mirror_m3(m):
w = [None]*3
w[0] = m[0][2] + m[0][1] + m[0][0]
w[1] = m[1][2] + m[1][1] + m[1][0]
w[2] = m[2][2] + m[2][1] + m[2][0]
return w
def mirror_m2(m):
w = [None]*2
w[0] = m[0][1] + m[0][0]
w[1] = m[1][1] + m[1][0]
return w
def rotate_m3(m):
r1 = m[0][0] + m[1][0] + m[2][0]
r2 = m[0][1] + m[1][1] + m[2][1]
r3 = m[0][2] + m[1][2] + m[2][2]
w = [None]*3
w[0] = r3
w[1] = r2
w[2] = r1
return w
def rotate_m2(m):
r1 = m[0][1] + m[1][1]
r2 = m[0][0] + m[1][0]
w = [None]*2
w[0] = r1
w[1] = r2
return w
pattern = ".#./..#/###"
pattern = pattern.split("/")
for i in range(5):
print 'round pattern: ','/'.join(pattern)
n = []
if len(pattern) % 2 == 0:
print 'div by 2'
print 'len(pattern)=',len(pattern)
count = (len(pattern)/2 *3)
for j in range(0,len(pattern)-1,2): #rows
for k in range(0,len(pattern)-1,2): #cols
#print k,j
new_pattern = []
new_pattern.append(pattern[j][k:k+2])
new_pattern.append(pattern[j+1][k:k+2])
k = ''
for p in range(2):
if k != '':
break
if p == 1:
new_pattern = mirror_m2(new_pattern)
if '/'.join(new_pattern) in d:
k = '/'.join(new_pattern)
break
for q in range(3):
new_pattern = rotate_m2(new_pattern)
if '/'.join(new_pattern) in d:
k = '/'.join(new_pattern)
break
print k, "=>", d[k]
n.append(d[k])
w = []
print 'count:',count
print n
for i in range(count):
s = ''
l = count/3
for j in range(l):
s+= n[(i/3)*l+j].split('/')[i%3]
w.append(s)
pattern = '/'.join(w)
pattern = pattern.split('/')
continue
if len(pattern) % 3 == 0:
# div by 3
print 'div by 3'
print 'len(pattern)=',len(pattern)
count = (len(pattern)/3*4)
for j in range(0,len(pattern)-1,3): #rows
for k in range(0,len(pattern)-1,3): #cols
#print k
new_pattern = []
new_pattern.append(pattern[j][k:k+3])
new_pattern.append(pattern[j+1][k:k+3])
new_pattern.append(pattern[j+2][k:k+3])
print 'new_pattern: ',new_pattern
k = ''
for p in range(2):
if k != '':
break
#flip non flip
if p == 1:
new_pattern = mirror_m3(new_pattern)
if '/'.join(new_pattern) in d:
#print 'found: ',new_pattern
k = '/'.join(new_pattern)
break
for q in range(3):
new_pattern = rotate_m3(new_pattern)
if '/'.join(new_pattern) in d:
k = '/'.join(new_pattern)
break
print k, "=>", d[k]
n.append(d[k])
w = []
print 'n:',n
print 'count:',count
for i in range(count):
s = ''
for j in range(count/4):
l = count/4
s+= n[(i/4)*l+j].split('/')[i%4]
w.append(s)
#print 'w:','/'.join(w)
pattern = '/'.join(w)
pattern = pattern.split('/')
continue
#print pattern
print sum(x == '#' for x in ''.join(pattern))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment