Ejemplo n.º 1
0
        public override string Solve(string input, bool part2)
        {
            //input = "123456789012";
            Console.WriteLine("Image Analizer v1.0");
            Console.WriteLine("Data recieved");
            width  = ConsoleAssist.GetUserInput("Please enter the width of the image");
            height = ConsoleAssist.GetUserInput("Please enter the height of the image");
            Console.WriteLine("Constructing image...");
            CreateImage(LoadImageData(input));
            if (part2)
            {
                return(Render());
            }
            int leastNulAmount = -1;
            int leastNulLayer  = -1;

            Console.WriteLine("Analyzing image...");
            for (int i = 0; i < image.Length; i++)
            {
                int[] statistics = GetLayerStatistics(image[i]);
                if (statistics[0] < leastNulAmount || leastNulAmount < 0)
                {
                    leastNulAmount = statistics[0];
                    leastNulLayer  = i;
                }
            }
            int[] layerStatistic = GetLayerStatistics(image[leastNulLayer]);
            return((layerStatistic[1] * layerStatistic[2]).ToString());
        }
Ejemplo n.º 2
0
 private void PrintUniverse(int wLayer, bool useAlt)
 {
     Console.WriteLine();
     try
     {
         //get the given universe and iterate over it's space to print it's state
         var universe = useAlt ? altMultiverse[wLayer] : multiverse[wLayer];
         for (int z = 0; z < zDim; z++)
         {
             for (int y = 0; y < yDim; y++)
             {
                 for (int x = 0; x < xDim; x++)
                 {
                     Console.Write(universe[z][y][x] ? '#' : '.');
                 }
                 Console.WriteLine();
             }
             Console.WriteLine();
             Console.WriteLine("Z=" + z);
             Console.WriteLine();
         }
     }
     catch
     {//universe doesn't exist
         for (int y = 0; y < yDim; y++)
         {
             Console.WriteLine(ConsoleAssist.Center("<EMPTY>", xDim, '-'));
         }
     }
 }
Ejemplo n.º 3
0
        public override string Solve(string input, bool part2)
        {
            var plan = GetLines(input);

            arrival = long.Parse(plan[0]);
            long time = arrival;

            rowWidth = arrival.ToString().Length + 2;

            //get all specified shuttle infos
            List <ShuttleBus> shuttles = new List <ShuttleBus>();

            string[] shuttleIDs = plan[1].Split(',');
            for (int i = 0; i < shuttleIDs.Length; i++)
            {
                if (shuttleIDs[i] == "x")
                {
                    continue;
                }
                int  id    = int.Parse(shuttleIDs[i]);
                long trips = arrival / (long)id;
                shuttles.Add(new ShuttleBus(id, i)
                {
                    Departures = part2 ? 0 : trips
                });
                long lastStopTime = trips * (long)id;
                if (lastStopTime < time)
                {
                    time = lastStopTime;
                }
                if (id.ToString().Length + 5 > rowWidth)
                {
                    rowWidth = id.ToString().Length + 7;
                }
            }

            //Print the header
            Console.CursorLeft = rowWidth;
            foreach (var shuttle in shuttles)
            {
                Console.Write("|" + ConsoleAssist.Center("Bus: " + shuttle.ID, rowWidth));
            }
            Console.WriteLine("|");
            //start the right routine
            if (part2)
            {
                return(GetOrdered(shuttles));
            }
            else
            {
                return(GetFirstDepart(shuttles, time));
            }
        }
Ejemplo n.º 4
0
        private string GetFirstDepart(List <ShuttleBus> shuttles, long time)
        {
            long firstShuttle          = 0;
            long departure             = 0;
            List <ShuttleBus> departed = new List <ShuttleBus>();

            //Iterate over time while shuttles need to depart
            do
            {
                Console.Write(time.ToString().PadLeft(rowWidth, time == arrival ? '=' : ' '));
                bool hasDeparture = false;
                //iterate over the shuttles
                for (int i = 0; i < shuttles.Count; i++)
                {
                    ShuttleBus shuttle = shuttles[i];
                    //check if our shuttle currently departs
                    if (time == shuttle.Departures * (long)shuttle.ID)
                    {
                        hasDeparture = true;
                        Console.Write("|" + ConsoleAssist.Center("D", rowWidth));
                        //are we at the bus stop yet?
                        if (time >= arrival)
                        {
                            if (departed.Count == 0)
                            {//this is the first bus, we can catch and it's departure time
                                firstShuttle = (long)shuttle.ID;
                                departure    = time;
                            }
                            //track this shuttle as departed
                            departed.Add(shuttle);
                        }
                        ++shuttle.Departures;
                    }
                    else
                    {
                        Console.Write("|" + ConsoleAssist.Center(".", rowWidth));
                    }
                }
                //if the time has no departure, reuse this line to save vertical screen space
                if (!hasDeparture && time != arrival)
                {
                    Console.CursorLeft = 0;
                }
                else
                {
                    Console.WriteLine("|");
                }
                time++;
                //check if all shuttles are departed
            } while (shuttles.Except(departed).Count() > 0);

            return(string.Format("Departing: Shuttle {0} @{1} Result: {2}", firstShuttle, departure, firstShuttle * (departure - arrival)));
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.Title          = "Advent of Code - Initializing";
            if (args.Length <= 0 || !TryGetLibrary(args[0]))
            {
                Console.Title = "Advent of Code - Set Year";
                while (true)
                {
                    int userYear = ConsoleAssist.GetUserInput("No year library specified. Enter a year number:");
                    if (TryGetLibrary(Directory.GetCurrentDirectory() + "\\" + userYear))
                    {
                        break;
                    }
                }
            }


            while (true)
            {
                Console.Clear();
                if (message != "")
                {
                    Console.WriteLine(message);
                }
                Console.Title = $"Advent Of Code {year} - Day Select.";
                int dayNr = ConsoleAssist.GetUserInput(
                    $"Advent Of Code {year}\r\n" +
                    "Enter the number of the Day to use\r\n" +
                    "Use Ctrl+C or enter 0 to quit.", false);
                message = "";

                if (dayNr <= 0)
                {
                    return;
                }

                if (!RunDay(dayNr, out message))
                {
                    continue;
                }

                Console.WriteLine("Done! Press enter to return to start.");
                while (Console.ReadKey(true).Key != ConsoleKey.Enter) /*that is not the key i want*/ } {
        }
    }
Ejemplo n.º 6
0
        private int ConwayStep()
        {
            AdjustBorder();
            int fields = 0;

            //Do conway for each universe
            for (int w = 0; w < wDim; w++)
            {
                fields += ConwayLayer(w);
                PrintUniverse(w, true);
            }

            Console.WriteLine();
            Console.WriteLine(ConsoleAssist.Center("", xDim, '-'));
            Console.WriteLine();

            //swap out the active multiverse with the alternative
            //we don't regenerate the alternative multiverse. Why thjrow away, what's perfectly fine for recycling? (Also Runtime stuff idk)
            var bufUniverse = multiverse;

            multiverse    = altMultiverse;
            altMultiverse = bufUniverse;
            return(fields);
        }
Ejemplo n.º 7
0
        public override string Solve(string input, bool part2)
        {
            //The input contains criterias, my ticket and other tickets, split and get criterias
            List <string> inputGroups = GetGroupedLines(input);

            criterias = GetCriteria(inputGroups[0]);

            //Split for each ticket and skip headline
            int           invalidSum = 0;
            List <string> tickets    = GetLines(inputGroups[2]);

            tickets.RemoveAt(0);
            int[] myTicket =
                GetLines(inputGroups[1])[1].
                Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).
                Select(x => int.Parse(x)).ToArray();
            //initialize a list keeping track of all possible names for each position
            foreach (var field in myTicket)
            {
                possibleNames.Add(criterias.Keys.ToList());
            }

            //Analyze the tickets
            ConsoleAssist assis = new ConsoleAssist();

            foreach (string ticket in tickets)
            {
                Console.CursorLeft = 0;
                Console.Write(assis.GetNextProgressChar());
                invalidSum += ValidateTicket(ticket);
            }
            Console.WriteLine();

            //Order the fields by ambiguity (unambiguous -> most ambiguous)
            var orderedFields = possibleNames.OrderBy(x => x.Count).ToList();

            for (int i = 0; i < possibleNames.Count; i++)
            {//get the next unambiguous field(first after all already done)
                var current = orderedFields.First(x => orderedFields.IndexOf(x) >= i && x.Count == 1);
                //Remove it's name from all other field lists
                foreach (var nameList in possibleNames)
                {
                    if (nameList == current)
                    {
                        continue;
                    }
                    nameList.Remove(current[0]);
                }
            }

            //print my ticket with the right names and multiply all values starting with departure.
            long product = 1;

            for (int i = 0; i < myTicket.Length; i++)
            {
                if (possibleNames[i].Count > 1)
                {
                    return("Ambiguous field name detected!");
                }
                Console.WriteLine(possibleNames[i][0] + ": " + myTicket[i]);
                if (possibleNames[i][0].StartsWith("departure", StringComparison.InvariantCultureIgnoreCase))
                {
                    product *= myTicket[i];
                }
            }

            if (part2)
            {
                return("Ticket Product:" + product);
            }
            else
            {
                return("Invalid Tickets: " + invalidSum);
            }
        }
Ejemplo n.º 8
0
        private string GetOrdered(List <ShuttleBus> shuttles)
        {
            bool orderedDeparture = true;
            long time             = 0;
            //our reference shuttle (we need to go in the specified order, hence the constant)
            const int         reference     = 0;
            List <ShuttleBus> trackedDepart = new List <ShuttleBus>();

            trackedDepart.Add(shuttles[0]);
            long trackDepartCount = 0;
            long stepSize         = 1;

            rowWidth = 4;
            //progress over time loop
            do
            {
                orderedDeparture = true;
                Console.Write(time.ToString().PadLeft(rowWidth));
                List <ShuttleBus> departed = new List <ShuttleBus>();
                //check each Shuttle for this step
                for (int i = 0; i < shuttles.Count; i++)
                {
                    ShuttleBus shuttle = shuttles[i];
                    //get the departures and make sure it is not before the required Time (time + offset)
                    shuttle.Departures = time / (long)shuttle.ID;
                    while (time + (long)(shuttle.Offset - shuttles[reference].Offset) > shuttle.Departures * (long)shuttle.ID)
                    {
                        ++shuttle.Departures;
                    }
                    //Check whether the shuttle departs at this time + offset timestamp (offset is always relative to our reference)
                    if (time + (long)(shuttle.Offset - shuttles[reference].Offset) == shuttle.Departures * (long)shuttle.ID)
                    {
                        Console.Write("|" + ConsoleAssist.Center(shuttle.Offset.ToString(), rowWidth));
                        //Create a list of all shuttles that departed in the right order
                        if (i == departed.Count)
                        {
                            departed.Add(shuttle);
                        }
                    }
                    else
                    {//the shuttle didn't depart as required
                        Console.Write("|" + ConsoleAssist.Center(".", rowWidth));
                        orderedDeparture = false;
                    }
                }
                //check if we had another shuttle departing
                if (departed.Count > (trackedDepart?.Count ?? 0))
                {
                    if (departed.Except(trackedDepart).Count() == departed.Count - trackedDepart.Count)
                    {//adjust the step size and refresh our found departures
                        stepSize = MathHelper.LeastCommonMultiple(stepSize, trackedDepart.Last().ID);
                        Console.Write("|\t" + stepSize);
                        trackDepartCount = time;
                        trackedDepart    = departed;
                    }
                }
                Console.WriteLine("|");

                //Go to the next time where the found shuttles depart together
                time += stepSize;
            } while (!orderedDeparture);

            return("First ordered Departure at " + (time + (shuttles[0].Offset - shuttles[reference].Offset) - stepSize));
        }