Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace Infi
- {
- public static class Program
- {
- /// <summary>
- /// Defines the entry point of the application.
- /// </summary>
- public static void Main()
- {
- Console.WriteLine($"De uitkomst van deel 1 is: {Infi(GetInput())}");
- Console.ReadLine();
- }
- /// <summary>
- /// Infi AoC challenge, bereken de clashes, visualiseer daarna de clashes.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <returns></returns>
- private static int Infi(string input)
- {
- var locationlist = new List<int[]>();
- var robotList = new List<int[]>();
- var clashLocations = new List<int[]>();
- (robotList, locationlist) = ModifyInput(input);
- var robotSwitcher = 0;
- var maxWidth = 0;
- foreach(var location in locationlist)
- {
- robotList[robotSwitcher][0] += location[0];
- robotList[robotSwitcher][1] += location[1];
- if (robotList[0][0] == robotList[1][0] && robotList[0][1] == robotList[1][1])
- {
- clashLocations.Add(new int[] { robotList[0][0], robotList[0][1] });
- maxWidth = Math.Max(maxWidth, robotList[0][0]);
- }
- robotSwitcher = (robotSwitcher + 1) % robotList.Count;
- }
- // Deel 2
- DrawMap(clashLocations, maxWidth);
- return clashLocations.Count;
- }
- /// <summary>
- /// Draws the map.
- /// </summary>
- /// <param name="locations">The locations.</param>
- /// <param name="maxWidth">The maximum width.</param>
- private static void DrawMap(List<int[]> locations, int maxWidth)
- {
- var map = new Dictionary<int, string[]>();
- foreach (var location in locations)
- {
- if (!map.ContainsKey(location[1])) {
- map.Add(location[1], new string[maxWidth + 1]);
- for (int i = 0; i < map[location[1]].Length; i++)
- {
- map[location[1]][i] = " ";
- }
- }
- map[location[1]][location[0]] = "#";
- }
- foreach (var key in map.Keys.OrderBy(x => x))
- {
- foreach (var arrValue in map[key])
- {
- Console.Write(arrValue);
- }
- Console.WriteLine();
- }
- }
- /// <summary>
- /// Modifies the input.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <returns>two lists, one with the robots, the second the locations</returns>
- private static (List<int[]> robotList, List<int[]> locationlist) ModifyInput(string input) {
- var locationlist = new List<int[]>();
- var robotList = new List<int[]>();
- bool robot = false;
- bool location = false;
- var start = 0;
- var length = 0;
- for (int i = 0; i < input.Length; i++)
- {
- if (input[i].Equals('['))
- {
- start = i + 1;
- robot = true;
- }
- if (input[i].Equals('('))
- {
- start = i + 1;
- location = true;
- }
- if ((input[i].Equals(']') && robot) || (input[i].Equals(')') && location))
- {
- length = i - start;
- var locationString = input.Substring(start, length);
- var locationArr = locationString.Split(',').Select(x => int.Parse(x)).ToArray();
- if(robot)
- {
- robotList.Add(locationArr);
- robot = false;
- }
- else if (location)
- {
- locationlist.Add(locationArr);
- location = false;
- }
- }
- }
- return (robotList, locationlist);
- }
- /// <summary>
- /// Gets the input.
- /// </summary>
- /// <returns></returns>
- private static string GetInput() => File.ReadLines(@"..\..\..\Input\Infi.txt").First();
- }
- }
Add Comment
Please, Sign In to add comment