-
Notifications
You must be signed in to change notification settings - Fork 0
/
star2.py
34 lines (25 loc) · 1.26 KB
/
star2.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
import numpy as np
from scipy.signal import convolve2d
def enhance(data, n_times):
algorithm = [int(x == '#') for x in data[0]]
img = np.array([[int(x == '#') for x in y] for y in data[2:]])
matrix = np.array([[2**0,2**1,2**2], [2**3,2**4,2**5], [2**6,2**7,2**8]])
fillvalue = 0
for _ in range(n_times):
idx = convolve2d(img, matrix, fillvalue=fillvalue)
img = np.vectorize(lambda x: algorithm[x])(idx)
fillvalue = algorithm[0] if fillvalue == 0 else algorithm[2**9-1]
return img
dataSample = """\
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
#..#.
#....
##..#
..#..
..###\
"""
# data = dataSample
data = open('data.txt', 'r', encoding='utf-8').read()
data = data.splitlines()
img = enhance(data, 50)
print(sum([sum(x) for x in img]))