There’s a YouTube channel called The Coding Train that has done a lot of programming challenges in the past. In my time after being laid off due to the Covid-19 Pandemic, I’m going to try to take on some of the coding challenges on the channel. My goal is to only watch the set-up and possibly end results of his challenge videos in order to not see his solution to the problem so I can work out a solution on my own (i.e. not looking at the code he writes or watching him work out the problems in the video).
Similarly to his videos, I am not trying to make a perfectly refined/polished/optimized solution to each challenge. The idea (for me, at least), is to go for breadth, not depth. I could spend a week polishing and perfecting each challenge, but I feel that most of the learning will happen in the basic functionality, not the refinement.
You might wonder why I choose to use MATLAB for these challenges. For one, I know MATLAB the best. But also, MATLAB has a lot of built in tools to make visualization easy, as well as native support for 2D arrays and matrix math, which simplifies a lot of calculations and data structures. MATLAB does have some drawbacks, such as execution time, but generally this hasn’t been much of a problem
Challenge 1: Starfield
Original Challenge Video: https://youtu.be/17WoOqgXsRM
Challenge 2: Menger Sponge
Original Challenge Video: https://youtu.be/LG8ZK-rRkXo
This one was actually extremely easy to code the basics. I just worked with the centerpoints of each block as x/y/z/w pairs (where “w” is the width of the block) and manipulated and added center points and didn’t make the blocks until the end. It did take the better part of a day to render the 3rd division, since I have not optimized the code to only draw external surfaces.
Challenge 3: Snake
Original Challenge Video: https://youtu.be/AaGK-fj-BAM
Testing wall failure mode:
Testing self-intersection failure mode:
Testing win condition (All available blocks being occupied by the snake). I’m not very good at snake, so the only way I could manage to win was by making a 2×2 play area and going in circles:
Challenge 4: Purple Rain
Original Challenge Video: https://youtu.be/KkyIDI6rQJI
Challenge 5: Space Invaders
Original Challenge Video: https://youtu.be/biN3v3ef-Y0
Testing Lose Condition:
Testing Win Condition:
Unfortunately, the MATLAB imshow() function is not very fast and limited me to ~18-20 frames per second. So to make it more consistent, I limited the game to 15 frames per second. It turns out that I should not have been using imshow(), but instead image(). This function runs much faster (uncapped up to 200 consistent frames per second) Also, there are some inconsistencies with pressing multiple keys at once. This could be fixed with a better input buffer, but the solution I came up with works well enough for now. Maybe if there are future challenges that require more accurate key input tracking I will make a better input method.
Challenge #6: Mitosis
Original Challenge Video: https://youtu.be/jxGS3fKPKJA
Most of my time on this was spent figuring out how to work the interactivity of the mouse with the image objects and how to handle the overall structure of creating split cells and destroying parent cells once they’re split (it turns out destroying and recreating each object each frame is a bad idea, who woulda thought?).
The MATLAB video writer tool does not capture the mouse, so unfortunately you can’t see my mouse position, but rest assured that each cell split is caused by my mouse click (you can check the code if you want)
Challenge #7: Solar System (2D)
As in the challenge video, the purpose of this challenge wasn’t to get perfect physics or avoid collisions. I’m just using randomly generated positions and angles for all bodies. If I were to space things out enough that they were guaranteed not to intersect, they’d be so small you wouldn’t really see them, or there would be so few objects it wouldn’t look as cool.
Challenge #8: Solar System (3D)
Original Challenge Video: https://youtu.be/dncudkelNxw
Similar to the first part of this Solar System challenge, the point is not to have an accurate model of a solar system, it’s more about rotations and translations to produce the effect. In the first video of the challenge series, the host said he eventually wanted to get to Keplerian motion, but peeking ahead it looks like he never gets there. I’m not sure if it’s really worth it for me to do that on my own, since it would just be plugging an ODE into MATLAB’s ode45 function and then plotting the results, which is neither hard nor exciting. This challenge was probably harder than the ODE since I had to work to fake all of the mechanics, instead of just letting the equations run the show.
Challenge #10: Maze Generator
This first video uses plain recursion to step through the algorithm. Unfortunately, this quickly runs into the MATLAB recursion limit (default 500) with larger board sizes. Next, I will implement the algorithm through use of linked lists to get larger board sizes
This second video uses linked lists to coordinate the stepping of the algorithm so it can run much larger game boards.
Challenge #11: Perlin Noise Terrain
Original Challenge Video: https://youtu.be/IKB1hWWedMk
Challenge #12: Lorenz Attractor
Original Challenge Video: https://youtu.be/f0lkz2gSsIk
I could have used ode45 for this, but the Wikipedia page on the Lorenz Attractor has example MATLAB code using ode45, so it would have been even less work for me to just copy/paste that code. So instead, I just used a very basic method (Euler’s Method) with a small time step to approximate the solution.
Challenge # 13: Reaction Diffusion
Original Challenge Video: https://youtu.be/BV9ny785UNc
This final video has the same setup as the first video (10×10 starting blob), but has a much larger “world.” Also I added some random noise to the “feed” and “kill” rates each loop, so its evolution is a lot more random. These are actually really fun to play with and maybe eventually I’ll mess around with the colors, but just displaying these videos takes around 85% of the total time (i.e. the “drawnow” function). I’ve done everything I can to increase the speed (set axis limits manually, reduce the bit-depth by going to a b/w 8-bit image, etc.) and this is as fast as I can make it run.
Challenge #14 and #15: Fractal Tree
Challenge #16: L-System Fractal Tree
Original Challenge Video: https://youtu.be/E1B4UoSQMFw
Challenge #17: Space Colonization Fractal Tree
Original Challenge Video: https://youtu.be/kKT0v3qhIQY
There’s an interesting scenario that is seen in the 2nd video, in which two attractor points are nearly equidistant from a tree node, which means the attraction ends up cancelling out and the new tree nodes created end up perpendicular to the direction of the attractor. This creates a back and forth “wiggle” of new nodes that end up not getting closer to either attractor, so an iteration limit was imposed, after which all unused attractors are destroyed.
Challenge #18: Space Colonization Fractal Tree 3D
Original Challenge Video: https://youtu.be/JcopTKXt8L8
Challenge #19: Superellipse
Original Challenge Video: https://youtu.be/z86cx2A4_3E
Challenge #20: 3D Cloth Simulation
Original Challenge Video: https://youtu.be/jrk_lOg_pVA
This was a bit of a “2-in-1” challenge since the original challenge used a physics engine library, but that library is not available for MATLAB. Since the challenge only used point masses and springs, I wrote my own basic setup to handle those objects. Doing that all in the format I chose definitely sacrificed execution speed, but it made it easy to write. In the future, if I need to do more intensive physics simulations, I’ll either look in to MATLAB libraries for physics engines, or make a more robust basic engine on my own (that’s actually something I want to do and am interested in, I’ve just never needed one).
Challenge #21: Mandelbrot Set
Original Challenge Video: https://youtu.be/6z7GQewK-Ks
I spent a lot of time messing around with renders of the Set. If you click the images, they’ll open the full-size 20,000 x 20,000 renders of the images.
Challenge #29: “Smart” Rockets
Original Challenge Video: https://youtu.be/bGz7mv2vD6g
I messed around a bit with the fintess function and mutation to try to get good results. The original challenge video had the rockets stop when they touched a wall or obstacle, but I found that this slowed down the evolution process a lot. Also, the original challenge didn’t include any fitness consideration for the speed at which a rocket reached the target, so I added that. In the video, and in the fitness measurements below, you can see that the maximum fitness was slowly increasing over time as the rockets learned to achieve their goal faster. I called this section “Smart” rockets because they really weren’t smart at all. The genetic algorithm essentially just throws a bunch of random noise at the problem until it finds a solution that works, which is both painfully slow, and also not smart.
If I find the time, I might re-run the simulation where instead of the rockets bouncing off of surfaces, the surface reduces their velocity to 0 (i.e. hitting a side wall will reduce their x-velocity to 0, and hitting a floor/ceiling will reduce their y-velocity to 0.
Challenge #47: Pixel Sorting
Original Challenge Video: https://youtu.be/JUDYkxU6J0o
Challenge #50.1: Bubble Packing
Original Challenge Video: https://youtu.be/QHEQuoIKgNE
Challenge #50.2: Color Bubble Packing
Original Challenge Video: https://youtu.be/ERQcYaaZ6F0