Advertisement
Guest User

AoC_2016_DayEleven

a guest
Dec 11th, 2016
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace AdventOfCode_Solutions
  5. {
  6.     class DayEleven_1
  7.     {
  8.         const string downCommand = "DOWN";
  9.         const string upCommand = "UP";
  10.         const string elevator = "EE";
  11.         const string strontiumGenerator = "SG";
  12.         const string strontiumMicrochip = "SM";
  13.         const string plutoniumGenerator = "PG";
  14.         const string plutoniumMicrochip = "PM";
  15.         const string thuliumGenerator = "TG";
  16.         const string thuliumMicrochip = "TM";
  17.         const string rutheniumGenerator = "RG";
  18.         const string rutheniumMicrochip = "RM";
  19.         const string curiumGenerator = "CG";
  20.         const string curiumMicrochip = "CM";
  21.         const string eleriumGenerator = "EG";
  22.         const string eleriumMicrochip = "EM";
  23.         const string dilithiumGenerator = "DG";
  24.         const string dilithiumMicrochip = "DM";
  25.         const string empty = "--";
  26.         public static List<List<string>> building;
  27.         public static int currentFloor = 0;
  28.         public static int totalMoves = 0;
  29.         public static Cargo[] cargoList = new Cargo[14];
  30.  
  31.         internal static void Run()
  32.         {
  33.             building = new List<List<string>>();
  34.             for (int index = 0; index < 4; index++)
  35.             {
  36.                 building.Add(new List<string>());
  37.                 for (int stringIndex = 0; stringIndex < 15; stringIndex++)
  38.                 {
  39.                     building[index].Add(empty);
  40.                 }
  41.             }
  42.             building[0][0] = elevator;
  43.             cargoList[0] = new Cargo(strontiumGenerator, 0);
  44.             cargoList[1] = new Cargo(strontiumMicrochip, 0);
  45.             cargoList[2] = new Cargo(plutoniumGenerator, 0);
  46.             cargoList[3] = new Cargo(plutoniumMicrochip, 0);
  47.             cargoList[4] = new Cargo(thuliumGenerator, 1);
  48.             cargoList[5] = new Cargo(thuliumMicrochip, 2);
  49.             cargoList[6] = new Cargo(rutheniumGenerator, 1);
  50.             cargoList[7] = new Cargo(rutheniumMicrochip, 1);
  51.             cargoList[8] = new Cargo(curiumGenerator, 1);
  52.             cargoList[9] = new Cargo(curiumMicrochip, 1);
  53.             cargoList[10] = new Cargo(eleriumGenerator, 0);
  54.             cargoList[11] = new Cargo(eleriumMicrochip, 0);
  55.             cargoList[12] = new Cargo(dilithiumGenerator, 0);
  56.             cargoList[13] = new Cargo(dilithiumMicrochip, 0);
  57.  
  58.             while (true)
  59.             {
  60.                 Draw();
  61.                 string command = Console.ReadLine().ToUpper();
  62.                 if (command == upCommand || command == downCommand)
  63.                 {
  64.                     Console.Write("Cargo One: ");
  65.                     Cargo cargoOne = Cargo.Find(Console.ReadLine());
  66.                     Console.Write("Cargo Two: ");
  67.                     Cargo cargoTwo = Cargo.Find(Console.ReadLine());
  68.                     if ((cargoOne == null || cargoOne.myFloor == currentFloor) && (cargoTwo == null || cargoTwo.myFloor == currentFloor))
  69.                     {
  70.                         MoveCargo(cargoOne, cargoTwo, command);
  71.                     }
  72.                 }
  73.                 else if (command == "-")
  74.                 {
  75.                     totalMoves--;
  76.                 }
  77.                 else if (command == "+")
  78.                 {
  79.                     totalMoves++;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         private static void Draw()
  85.         {
  86.             Console.Clear();
  87.             Console.WriteLine("Advent of Code 2016");
  88.             Console.WriteLine();
  89.             for (int index = 3; index >= 0; index--)
  90.             {
  91.                 Console.Write("F" + index + " ");
  92.                 for (int stringIndex = 0; stringIndex < 15; stringIndex++)
  93.                 {
  94.                     if (stringIndex < 14)
  95.                     {
  96.                         Console.Write(building[index][stringIndex] + " ");
  97.                     }
  98.                     else
  99.                     {
  100.                         Console.WriteLine(building[index][stringIndex]);
  101.                     }
  102.                 }
  103.             }
  104.             Console.WriteLine("Moves: " + totalMoves);
  105.         }
  106.  
  107.         private static void MoveCargo(Cargo cargoOne, Cargo cargoTwo, string command)
  108.         {
  109.             if (command.Equals(upCommand))
  110.             {
  111.                 if (currentFloor < 3 && (cargoOne != null || cargoTwo != null))
  112.                 {
  113.                     if (cargoOne != null)
  114.                     {
  115.                         cargoOne.MoveUp();
  116.                     }
  117.  
  118.                     if (cargoTwo != null)
  119.                     {
  120.                         cargoTwo.MoveUp();
  121.                     }
  122.  
  123.                     building[currentFloor][0] = empty;
  124.                     currentFloor++;
  125.                     building[currentFloor][0] = elevator;
  126.                     totalMoves++;
  127.                 }
  128.             }
  129.             else
  130.             {
  131.                 if (currentFloor > 0 && (cargoOne != null || cargoTwo != null))
  132.                 {
  133.                     if (cargoOne != null)
  134.                     {
  135.                         cargoOne.MoveDown();
  136.                     }
  137.  
  138.                     if (cargoTwo != null)
  139.                     {
  140.                         cargoTwo.MoveDown();
  141.                     }
  142.  
  143.                     building[currentFloor][0] = empty;
  144.                     currentFloor--;
  145.                     building[currentFloor][0] = elevator;
  146.                     totalMoves++;
  147.                 }
  148.             }
  149.         }
  150.  
  151.         public class Cargo
  152.         {
  153.             static int lastSlot = 0;
  154.             public int mySlot;
  155.             public int myFloor;
  156.             public string myID;
  157.  
  158.             public Cargo(string id, int floor)
  159.             {
  160.                 myID = id;
  161.                 myFloor = floor;
  162.                 lastSlot++;
  163.                 mySlot = lastSlot;
  164.                 building[myFloor][mySlot] = myID;
  165.             }
  166.  
  167.             public void MoveDown()
  168.             {
  169.                 building[myFloor][mySlot] = empty;
  170.                 myFloor--;
  171.                 building[myFloor][mySlot] = myID;
  172.             }
  173.  
  174.             public void MoveUp()
  175.             {
  176.                 building[myFloor][mySlot] = empty;
  177.                 myFloor++;
  178.                 building[myFloor][mySlot] = myID;
  179.             }
  180.  
  181.             public static Cargo Find(string id)
  182.             {
  183.                 foreach (Cargo cargo in cargoList)
  184.                 {
  185.                     if (cargo.myID.ToUpper() == id.ToUpper())
  186.                     {
  187.                         return cargo;
  188.                     }
  189.                 }
  190.                 return null;
  191.             }
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement