animation library
This commit is contained in:
parent
03ce126e23
commit
e5969b04f8
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
|
gif-*
|
||||||
# Created by https://www.toptal.com/developers/gitignore/api/python
|
# Created by https://www.toptal.com/developers/gitignore/api/python
|
||||||
# Edit at https://www.toptal.com/developers/gitignore?templates=python
|
# Edit at https://www.toptal.com/developers/gitignore?templates=python
|
||||||
|
|
||||||
|
49
2021/python/anim.py
Normal file
49
2021/python/anim.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
from matrix import get_size, pmx
|
||||||
|
import imageio
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
|
||||||
|
class Animate:
|
||||||
|
def __init__(self, mx, day="CHANGEME"):
|
||||||
|
self.mx= mx
|
||||||
|
_size = get_size(mx)
|
||||||
|
self.height = _size[0]
|
||||||
|
self.width = _size[1]
|
||||||
|
self.day = day
|
||||||
|
self.frames = [deepcopy(mx)]
|
||||||
|
|
||||||
|
def add_frame(self, frame):
|
||||||
|
self.frames.append(deepcopy(frame))
|
||||||
|
|
||||||
|
def print(self):
|
||||||
|
for f in self.frames:
|
||||||
|
pmx(f)
|
||||||
|
print("-")
|
||||||
|
|
||||||
|
|
||||||
|
def animate(self, save=False, or_frames=False):
|
||||||
|
last_file = 0
|
||||||
|
for f_idx, frame in enumerate(self.frames):
|
||||||
|
current = np.zeros_like(self.mx)
|
||||||
|
for y, row in enumerate(frame):
|
||||||
|
for x, col in enumerate(row):
|
||||||
|
current[y][x] = frame[y][x]
|
||||||
|
pmx(current)
|
||||||
|
|
||||||
|
fig, ax = plt.subplots()
|
||||||
|
ax.imshow(current, cmap=plt.cm.gray)
|
||||||
|
ax.axis("off")
|
||||||
|
if save:
|
||||||
|
plt.savefig(f"{self.day}/{f_idx:03}.png")
|
||||||
|
plt.close()
|
||||||
|
|
||||||
|
with imageio.get_writer(f"{self.day}/day{self.day}.gif", mode="I") as writer:
|
||||||
|
names = [f"{self.day}/{x:03}.png" for x in range(len(self.frames))]
|
||||||
|
for filename in names:
|
||||||
|
try:
|
||||||
|
image = imageio.imread(filename)
|
||||||
|
writer.append_data(image)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
@ -69,6 +69,7 @@ class Smoke:
|
|||||||
return prod(largest)
|
return prod(largest)
|
||||||
|
|
||||||
def animate(self, save=False):
|
def animate(self, save=False):
|
||||||
|
breakpoint()
|
||||||
largest = [x for x in self.by_center if x[0] in self.largest][-3:]
|
largest = [x for x in self.by_center if x[0] in self.largest][-3:]
|
||||||
current = np.zeros_like(self.mx)
|
current = np.zeros_like(self.mx)
|
||||||
shuf = self.by_center[:]
|
shuf = self.by_center[:]
|
||||||
|
@ -2,45 +2,37 @@ from matrix import get_size, pmx
|
|||||||
import imageio
|
import imageio
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from copy import deepcopy
|
|
||||||
|
|
||||||
|
|
||||||
class Animate:
|
class Animate:
|
||||||
def __init__(self, mx, day="CHANGEME"):
|
def __init__(self, mx, day="CHANGEME"):
|
||||||
self.mx= mx
|
self.mx = mx
|
||||||
|
self.day = day
|
||||||
_size = get_size(mx)
|
_size = get_size(mx)
|
||||||
self.height = _size[0]
|
self.height = _size[0]
|
||||||
self.width = _size[1]
|
self.width = _size[1]
|
||||||
self.day = day
|
self.f_count = -1
|
||||||
self.frames = [deepcopy(mx)]
|
|
||||||
|
|
||||||
def add_frame(self, frame):
|
def add_frame(self, frame):
|
||||||
self.frames.append(deepcopy(frame))
|
self.f_count += 1
|
||||||
|
print(self.f_count)
|
||||||
|
self.write_frame(frame)
|
||||||
|
|
||||||
def print(self):
|
def write_frame(self, frame):
|
||||||
for f in self.frames:
|
current = np.zeros_like(self.mx)
|
||||||
pmx(f)
|
for y, row in enumerate(frame):
|
||||||
print("-")
|
for x, col in enumerate(row):
|
||||||
|
current[y][x] = frame[y][x]
|
||||||
|
fig, ax = plt.subplots()
|
||||||
|
ax.imshow(current, cmap=plt.cm.gray)
|
||||||
|
ax.axis("off")
|
||||||
|
_figpath = (f"gif-{self.day}/{self.f_count:05}.png")
|
||||||
|
plt.savefig(_figpath)
|
||||||
|
plt.close()
|
||||||
|
|
||||||
|
def animate(self, frameskip=1):
|
||||||
def animate(self, save=False, or_frames=False):
|
with imageio.get_writer(f"gif-{self.day}/day{self.day}.gif", mode="I") as writer:
|
||||||
last_file = 0
|
names = [f"gif-{self.day}/{x:05}.png" for x in range(0,self.f_count, frameskip)]
|
||||||
for f_idx, frame in enumerate(self.frames):
|
print(names)
|
||||||
current = np.zeros_like(self.mx)
|
|
||||||
for y, row in enumerate(frame):
|
|
||||||
for x, col in enumerate(row):
|
|
||||||
current[y][x] = frame[y][x]
|
|
||||||
pmx(current)
|
|
||||||
|
|
||||||
fig, ax = plt.subplots()
|
|
||||||
ax.imshow(current, cmap=plt.cm.gray)
|
|
||||||
ax.axis("off")
|
|
||||||
if save:
|
|
||||||
plt.savefig(f"{self.day}/{f_idx:03}.png")
|
|
||||||
plt.close()
|
|
||||||
|
|
||||||
with imageio.get_writer(f"{self.day}/day{self.day}.gif", mode="I") as writer:
|
|
||||||
names = [f"{self.day}/{x:03}.png" for x in range(len(self.frames))]
|
|
||||||
for filename in names:
|
for filename in names:
|
||||||
try:
|
try:
|
||||||
image = imageio.imread(filename)
|
image = imageio.imread(filename)
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
from pprint import pprint as pp
|
from pprint import pprint as pp
|
||||||
|
|
||||||
|
from anim import Animate
|
||||||
import shared
|
import shared
|
||||||
import matrix
|
import matrix
|
||||||
|
|
||||||
MATRIX_SIZE = 500
|
MATRIX_SIZE = 400
|
||||||
#MATRIX_SIZE = 40 # sample
|
ATRIX_SIZE = 40 # sample
|
||||||
HALF = MATRIX_SIZE // 2
|
HALF = MATRIX_SIZE // 2
|
||||||
|
|
||||||
H = [HALF,HALF]
|
H = [HALF,HALF]
|
||||||
@ -25,6 +27,7 @@ DIRS = {
|
|||||||
|
|
||||||
def part1(steps):
|
def part1(steps):
|
||||||
field = matrix.matrix_of_size(MATRIX_SIZE,MATRIX_SIZE)
|
field = matrix.matrix_of_size(MATRIX_SIZE,MATRIX_SIZE)
|
||||||
|
anim = Animate(field, day="09")
|
||||||
|
|
||||||
for direction, amount in steps:
|
for direction, amount in steps:
|
||||||
# move HEAD
|
# move HEAD
|
||||||
@ -73,14 +76,18 @@ def part1(steps):
|
|||||||
|
|
||||||
T[0] += tY
|
T[0] += tY
|
||||||
T[1] += tX
|
T[1] += tX
|
||||||
|
anim.add_frame(field)
|
||||||
|
|
||||||
field[T[0]][T[1]] = 1 # mark visited tails
|
field[T[0]][T[1]] = 1 # mark visited tails
|
||||||
print(matrix.sum_matrix(field))
|
print(matrix.sum_matrix(field))
|
||||||
|
anim.animate()
|
||||||
|
|
||||||
|
|
||||||
def part2(steps):
|
def part2(steps):
|
||||||
field = matrix.matrix_of_size(MATRIX_SIZE,MATRIX_SIZE)
|
field = matrix.matrix_of_size(MATRIX_SIZE,MATRIX_SIZE)
|
||||||
|
anim = Animate(field, day="09")
|
||||||
S = [[HALF,HALF],] # HEAD ONLY
|
S = [[HALF,HALF],] # HEAD ONLY
|
||||||
|
_cur_frame = 0
|
||||||
for x in range(9):
|
for x in range(9):
|
||||||
S.append([HALF,HALF])
|
S.append([HALF,HALF])
|
||||||
|
|
||||||
@ -136,15 +143,18 @@ def part2(steps):
|
|||||||
|
|
||||||
S[knot_idx][0] += tY
|
S[knot_idx][0] += tY
|
||||||
S[knot_idx][1] += tX
|
S[knot_idx][1] += tX
|
||||||
|
if _cur_frame % 25 == 0:
|
||||||
|
anim.add_frame(field)
|
||||||
|
_cur_frame += 1
|
||||||
field[S[-1][0]][S[-1][1]] = 1 # mark visited tails
|
field[S[-1][0]][S[-1][1]] = 1 # mark visited tails
|
||||||
print(matrix.sum_matrix(field))
|
print(matrix.sum_matrix(field))
|
||||||
|
anim.animate()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
rows = [x.split(" ") for x in shared.load_rows(9)]
|
rows = [x.split(" ") for x in shared.load_rows(9)]
|
||||||
part1(rows)
|
#part1(rows)
|
||||||
part2(rows)
|
part2(rows)
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,3 +6,11 @@ R 4
|
|||||||
D 1
|
D 1
|
||||||
L 5
|
L 5
|
||||||
R 2
|
R 2
|
||||||
|
R 5
|
||||||
|
U 8
|
||||||
|
L 8
|
||||||
|
D 3
|
||||||
|
R 17
|
||||||
|
D 10
|
||||||
|
L 25
|
||||||
|
U 20
|
||||||
|
Loading…
Reference in New Issue
Block a user