class Program
{
static void Main(string[] args)
{
var file = File.ReadAllLines(@"../../../Input.txt");
//Part 1
Console.WriteLine(Solve(file, false));
//Part 2
Console.WriteLine(Solve(file, true));
}
static public string Solve(string[] lines, bool inOrder)
{
var nrCranes = (int)Math.Ceiling(lines[0].Length / 4.0);
var cranes = new List<char>[nrCranes];
for (var i = 0; i < nrCranes; i++)
{
cranes[i] = new List<char>();
}
var l = 0;
for (; l < lines.Length && lines[l][1] != '1'; l++)
{
var line = lines[l];
var crane = 0;
foreach (var crate in line.Chunk(4).Select(x => x[1]))
{
if (crate != ' ')
{
cranes[crane].Add(crate);
}
crane++;
}
}
l += 2;
for (; l < lines.Length; l++)
{
var line = lines[l];
var parts = line.Split(' ');
var count = int.Parse(parts[1]);
var from = cranes[int.Parse(parts[3]) - 1];
var to = cranes[int.Parse(parts[5]) - 1];
if (inOrder)
{
to.InsertRange(0, from.Take(count));
}
else
{
for (var j = 0; j < count; j++)
{
to.Insert(0, from[j]);
}
}
from.RemoveRange(0, count);
}
return string.Join("", cranes.Select(x => x[0]));
}
}