Untitled
unknown
csharp
3 years ago
3.1 kB
15
Indexable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace Day5
{
public class Move
{
public int Amount { get; set; }
public int From { get; set; }
public int To { get; set; }
}
internal class Program
{
public static List<string> Input => File.ReadAllLines(@"input.txt").ToList();
public static Dictionary<int, Stack<char>> Stacks => new Dictionary<int, Stack<char>>()
{
{ 1, new Stack<char>( new char[] { 'H', 'T', 'Z', 'D' } ) },
{ 2, new Stack<char>( new char[] { 'Q', 'R', 'W', 'T', 'G', 'C', 'S' } ) },
{ 3, new Stack<char>( new char[] { 'P', 'B', 'F', 'Q', 'N', 'R', 'C', 'H' } ) },
{ 4, new Stack<char>( new char[] { 'L', 'C', 'N', 'F', 'H', 'Z' } ) },
{ 5, new Stack<char>( new char[] { 'G', 'L', 'F', 'Q', 'S' } ) },
{ 6, new Stack<char>( new char[] { 'V', 'P', 'W', 'Z', 'B', 'R', 'C', 'S' } ) },
{ 7, new Stack<char>( new char[] { 'Z', 'F', 'J' } ) },
{ 8, new Stack<char>( new char[] { 'D', 'L', 'V', 'Z', 'R', 'H', 'Q' } ) },
{ 9, new Stack<char>( new char[] { 'B', 'H', 'G', 'N', 'F', 'Z', 'L', 'D' } ) },
};
public static List<Move> MoveList => Input.Skip(Input.FindIndex(x => x == "") + 1).Select(x => new Move()
{
Amount = int.Parse(x.Split(' ')[1]),
From = int.Parse(x.Split(' ')[3]),
To = int.Parse(x.Split(' ')[5]),
}).ToList();
static void Main(string[] args)
{
var Part1Stacks = Stacks;
var Part2Stacks = Stacks;
foreach (var move in MoveList)
{
//Part1
for (int i = 0; i < move.Amount; i++)
{
var crate = Part1Stacks[move.From].Pop();
Part1Stacks[move.To].Push(crate);
}
//Part2
var crates = Part2Stacks[move.From].PopRange(move.Amount).Reverse<char>().ToArray();
Part2Stacks[move.To].PushRange(crates);
}
foreach (var stack in Part1Stacks)
{
Console.Write(stack.Value.Peek());
}
foreach (var stack in Part2Stacks)
{
Console.Write(stack.Value.Peek());
}
Console.ReadLine();
}
}
public static class Extensions
{
public static List<T> PopRange<T>(this Stack<T> stack, int amount)
{
var result = new List<T>(amount);
while (amount-- > 0 && stack.Count > 0)
{
result.Add(stack.Pop());
}
return result;
}
public static void PushRange<T>(this Stack<T> source, IEnumerable<T> collection)
{
foreach (var item in collection)
source.Push(item);
}
}
}
Editor is loading...