You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
import numpy as np
|
|
|
|
|
|
def find_reflexion(pattern):
|
|
for i in range(1, len(pattern)):
|
|
reflexion = True
|
|
for j in range(i):
|
|
if (i+i-j-1) > (len(pattern)-1): continue
|
|
if not np.array_equal(pattern[j], pattern[i+i-j-1]): reflexion = False
|
|
if reflexion: return i
|
|
return 0
|
|
|
|
|
|
def find_smudged_reflexion(pattern):
|
|
for i in range(1, len(pattern)):
|
|
smudges = 0
|
|
for j in range(i):
|
|
if (i+i-j-1) > (len(pattern)-1): continue
|
|
for p in range(len(pattern[j])):
|
|
if pattern[i+i-j-1][p] != pattern[j][p]:
|
|
smudges += 1
|
|
if smudges == 1: return i
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
lines = open(sys.argv[1]).read()
|
|
patterns = lines[:-1].split('\n\n')
|
|
patterns = [[list(n) for n in p.split('\n')] for p in patterns]
|
|
|
|
res1, res2 = 0, 0
|
|
for pattern in patterns:
|
|
res1 += 100 * find_reflexion(pattern)
|
|
res2 += 100 * find_smudged_reflexion(pattern)
|
|
vert = np.transpose(pattern)
|
|
res1 += find_reflexion(vert)
|
|
res2 += find_smudged_reflexion(vert)
|
|
|
|
# challenge 1
|
|
res1 = str(res1)
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
res2 = str(res2)
|
|
print(f"challenge 2:\n{res2}\n")
|
|
|
|
|