1
+ import time
2
+ import sys , os
3
+ from util import Day
4
+
5
+ import numpy as np
6
+ import matplotlib .pyplot as plt
7
+ import matplotlib .animation as animation
8
+
9
+
10
+ class Arcade (Day ):
11
+ def __init__ (self , day , part ):
12
+ super ().__init__ (day , part )
13
+ self .concurrent = True
14
+ self .score = 0
15
+
16
+
17
+ def paint (self ):
18
+ self .canvas = {}
19
+ self .ball_x = 0
20
+ self .paddle_x = 0
21
+
22
+ while True :
23
+ x = self .execute_opcode (reset_pointer = False )
24
+ y = self .execute_opcode (reset_pointer = False )
25
+ z = self .execute_opcode (reset_pointer = False )
26
+
27
+ if z == 4 :
28
+ self .ball_x = x
29
+ self .input (- 1 if self .ball_x < self .paddle_x else 1 if self .ball_x > self .paddle_x else 0 )
30
+ if z == 3 :
31
+ self .paddle_x = x
32
+
33
+ if isinstance (z , Day ):
34
+ break
35
+ if x == - 1 and y == 0 :
36
+ #print("Score: ", z)
37
+ self .score = z
38
+ self .visualize ()
39
+ else :
40
+ self .canvas [(x , y )] = z
41
+ if len (self .canvas .keys ()) > 910 :
42
+ self .visualize ()
43
+ return self
44
+ def visualize (self ):
45
+
46
+ print ("Score: " , self .score )
47
+ #x, y = zip(*self.canvas.keys())
48
+ #painting = {0: " ", 1: "░", 2: "▒", 3: "▉", 4: "◓"}
49
+ #for j in range(min(y)-2, max(y)+1):
50
+ # for i in range(min(x), max(x)+1):
51
+ # for j in range(0, 25):
52
+ # for i in range(0, 45):
53
+ # sys.stdout.write(painting[self.canvas.get((i, j), 0)])
54
+ # sys.stdout.write("\n")
55
+ return self
56
+
57
+
58
+ if __name__ == "__main__" :
59
+ # part1 = Day(13, 1)
60
+
61
+ # part1.load(typing=int, sep=",")
62
+
63
+ # part1.concurrent = True
64
+
65
+ # canvas = paint(part1)
66
+
67
+ # print(sum([int(x == 2) for x in canvas.values()]))
68
+
69
+ part2 = Arcade (13 , 2 )
70
+
71
+ part2 .load (typing = int , sep = "," )
72
+
73
+ part2 .data [0 ] = 2
74
+ part2 .bake ()
75
+ part2 .mem_load ()
76
+
77
+ part2 .paint ()
78
+ #part2.visualize()
79
+
80
+ print (part2 .score )
81
+
82
+ # x, y = zip(*self.path.keys())
83
+
84
+ # 0 is an empty tile. No game object appears in this tile.
85
+ # 1 is a wall tile. Walls are indestructible barriers.
86
+ # 2 is a block tile. Blocks can be broken by the ball.
87
+ # 3 is a horizontal paddle tile. The paddle is indestructible.
88
+ # 4 is a ball tile. The ball moves diagonally and bounces off objects.
89
+
90
+ fig = plt .figure ()
91
+
92
+
93
+ def f (x , y ):
94
+ return np .sin (x ) + np .cos (y )
95
+
96
+ x = np .linspace (0 , 2 * np .pi , 120 )
97
+ y = np .linspace (0 , 2 * np .pi , 100 ).reshape (- 1 , 1 )
98
+ # ims is a list of lists, each row is a list of artists to draw in the
99
+ # current frame; here we are just animating one artist, the image, in
100
+ # each frame
101
+ ims = []
102
+ for i in range (60 ):
103
+ x += np .pi / 15.
104
+ y += np .pi / 20.
105
+ im = plt .imshow (f (x , y ), animated = True )
106
+ ims .append ([im ])
107
+
108
+ ani = animation .ArtistAnimation (fig , ims , interval = 50 , blit = True ,
109
+ repeat_delay = 1000 )
110
+
111
+ # ani.save('dynamic_images.mp4')
112
+
113
+ plt .show ()
0 commit comments