Skip to content

Instantly share code, notes, and snippets.

@VegaFromLyra
Created July 22, 2015 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VegaFromLyra/be7ad836717dc286343c to your computer and use it in GitHub Desktop.
Save VegaFromLyra/be7ad836717dc286343c to your computer and use it in GitHub Desktop.
Animation
using System;
using System.Text;
using System.Collections.Generic;
// A collection of particles is contained in a linear chamber. They all have the same speed, but some are headed toward the right and others are headed toward the left. These particles can pass through each other without disturbing the motion of the particles, so all the particles will leave the chamber relatively quickly.
// You will be given the initial conditions by a String init containing at each position an 'L' for a leftward moving particle, an 'R' for a rightward moving particle, or a '.' for an empty location. init shows all the positions in the chamber. Initially, no location in the chamber contains two particles passing through each other.
// We would like an animation of the process. At each unit of time, we want a string showing occupied locations with an 'X' and unoccupied locations with a '.'. Create a class Animation that contains a method animate that is given an int speed and a String init giving the initial conditions. The speed is the number of positions each particle moves in one time unit.
// The method will return an array of strings in which each successive element shows the occupied locations at the next time unit. The first element of the return should show the occupied locations at the initial instant (at time = 0) in the 'X', '.' format. The last element in the return should show the empty chamber at the first time that it becomes empty.
namespace Animation
{
public class Program
{
public static void Main(string[] args)
{
test(2, "..R....");
test(3, "RR..LRL");
test(2, "LRLR.LRLR");
test(10, "RLRLRLRLRL");
test(1, "...");
test(1, "LRRL.LR.LRR.R.LRRL.");
}
static void test(int speed, string init) {
Console.WriteLine("Output:");
animate(speed, init);
}
static void animate(int speed, string init) {
var chamber = getChamber(init);
var particleCount = getParticleCount(chamber);
Console.WriteLine(getInitSequence(init));
while(particleCount > 0) {
var animatedSequence = new char[init.Length];
var updatedChamber = new List<StringBuilder>();
for(int c = 0; c < init.Length; c++) {
updatedChamber.Add(new StringBuilder(""));
}
for(int i = 0; i < chamber.Count; i++) {
var particles = chamber[i];
for(int j = 0; j < particles.Length; j++) {
if (particles[j] == 'R') {
int newPos = i + speed;
if (newPos < init.Length) {
animatedSequence[newPos] = 'X';
updatedChamber[newPos].Append('R');
}
} else if (particles[j] == 'L') {
int newPos = i - speed;
if (newPos >= 0) {
animatedSequence[newPos] = 'X';
updatedChamber[newPos].Append('L');
}
}
}
}
for(int i = 0; i < animatedSequence.Length; i++) {
if (animatedSequence[i] == 0) {
animatedSequence[i] = '.';
}
}
Console.WriteLine(animatedSequence);
chamber = updatedChamber;
particleCount = getParticleCount(chamber);
}
}
static char[] getInitSequence(string s) {
var sequence = new char[s.Length];
for(int i = 0; i < s.Length; i++) {
sequence[i] = s[i] == '.' ? '.' : 'X';
}
return sequence;
}
static List<StringBuilder> getChamber(string s) {
var chamber = new List<StringBuilder>();
for(int i = 0; i < s.Length; i++) {
chamber.Add(new StringBuilder(""));
if (s[i] == 'L') {
chamber[i].Append('L');
} else if (s[i] == 'R') {
chamber[i].Append('R');
}
}
return chamber;
}
static int getParticleCount(List<StringBuilder> chamber) {
int particleCount = 0;
foreach(var particleSet in chamber) {
particleCount += particleSet.Length;
}
return particleCount;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment