Untitled

 avatar
unknown
csharp
3 years ago
3.2 kB
7
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[] { 'Z', 'N' } )  },
        //    { 2, new Stack<char>( new char[] { 'M', 'C', 'D' } )  },
        //    { 3, new Stack<char>( new char[] { 'P' } )  }
        //};

        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 newStacks = Stacks;

            foreach (var move in MoveList)
            {
                var crates = newStacks[move.From].PopRange(move.Amount);

                var reverseCreates = crates.Reverse<char>().ToArray();

                for (int i = 0; i < reverseCreates.Count(); i++)
                {
                    newStacks[move.To].Push(reverseCreates[i]);
                }

                //part1
                //for (int i = 0; i < move.Amount; i++)
                //{
                //    var crate = newStacks[move.From].Pop();
                //    newStacks[move.To].Push(crate);
                //}
            }
            foreach (var stack in newStacks)
            {
                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;
        }
    }



}
Editor is loading...