-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2019_08_animation.py
42 lines (34 loc) · 1.17 KB
/
2019_08_animation.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
from aoc import read_file, timer
from collections import defaultdict
from os import path
from PIL import Image, ImageDraw
def extract_layers(raw, size):
layers = defaultdict(list)
layer = -1
for i in range(len(raw)):
if not i % size:
layer += 1
layers[layer].append(raw[i])
return layers
def print_image(layers):
scale = 20
for layer in range(100):
img = Image.new('RGBA', (25*scale, 6*scale), (255, 0, 0, 0))
draw = ImageDraw.Draw(img)
for y in range(6):
for x in range(25):
pos = x + y * 25
if layers[layer][pos] == 0:
draw.ellipse((x*scale, y*scale, x*scale+scale-1, y*scale+scale-1), fill=(255, 255, 255))
elif layers[layer][pos] == 1:
draw.ellipse((x*scale, y*scale, x*scale+scale-1, y*scale+scale-1), fill=(0, 0, 0))
file_path = path.join('images', f"BIOS{100-layer:03}.png")
img.save(file_path, 'PNG')
@timer
def solve():
input = read_file("08")
input = [int(x) for x in input[0]]
size = 25 * 6
layers = extract_layers(input, size)
print_image(layers)
solve()