Advertisement
Guest User

Advent of Code: Day 13 Solution

a guest
Dec 13th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.24 KB | None | 0 0
  1. public static class AoCDay13
  2. {
  3.     /// <summary>
  4.     /// Fold along the x axis given the y coordinate.
  5.     /// </summary>
  6.     /// <param name="_points"></param>
  7.     /// <param name="_y"></param>
  8.     /// <param name="_height"></param>
  9.     public static Point[] FoldX(Point[] _points, int _y)
  10.     {
  11.         HashSet<Point> _pointsList = new HashSet<Point>();
  12.         foreach (var _point in _points)
  13.         {
  14.             if (_point.Y > _y)
  15.             {
  16.                 Point _p = new Point(_point.X, _y + -(_point.Y - _y));
  17.                 _pointsList.Add(_p);
  18.             }
  19.             else
  20.             {
  21.                 _pointsList.Add(new Point(_point.X, _point.Y));
  22.             }
  23.         }
  24.         return _pointsList.ToArray();
  25.     }
  26.  
  27.     /// <summary>
  28.     /// Fold along the x axis given the y coordinate.
  29.     /// </summary>
  30.     /// <param name="_points"></param>
  31.     /// <param name="_y"></param>
  32.     /// <param name="_height"></param>
  33.     public static Point[] FoldY(Point[] _points, int _x)
  34.     {
  35.         HashSet<Point> _pointsList = new HashSet<Point>();
  36.  
  37.         foreach (var _point in _points)
  38.         {
  39.             if (_point.X > _x)
  40.             {
  41.                 Point _p = new Point(_x + -(_point.X - _x), _point.Y);
  42.                 _pointsList.Add(_p);
  43.             }
  44.             else
  45.             {
  46.                 _pointsList.Add(new Point(_point.X, _point.Y));
  47.             }
  48.         }
  49.         return _pointsList.ToArray();
  50.     }
  51.  
  52.     public static Point[] Fold(Point[] _points, char _axis, int _value)
  53.     {
  54.         switch (_axis)
  55.         {
  56.             case 'x':
  57.                 return FoldY(_points, _value);
  58.                 break;
  59.             case 'y':
  60.                 return FoldX(_points, _value);
  61.             default:
  62.                 return _points;
  63.         }
  64.     }
  65.  
  66.     public static void Draw(Point[] _points, ref StringBuilder _sb, int _width, int _height)
  67.     {
  68.         for (int _y = 0; _y < _height / 10; _y++)
  69.         {
  70.             for (int _x = 0; _x < _width / 10; _x++)
  71.             {
  72.                 Point _point = new Point(_x, _y);
  73.                 if (_points.Contains(_point))
  74.                     Console.Write("#");
  75.                 else
  76.                 {
  77.                     Console.Write(" ");
  78.                 }
  79.                 // So many new lines makes the text shoot up too quick so delay it.
  80.                 // Plus, it looks cool =D
  81.                 Thread.Sleep(15);
  82.             }
  83.             Console.WriteLine("");
  84.         }
  85.     }
  86.  
  87.     public static (T[] _first, T[] _second) Split<T>(this T[] array, Func<T[], int> _func) {
  88.         return (array.Take(_func(array)).ToArray(), array.Skip(_func(array)).ToArray());
  89.     }
  90.  
  91.     public static void Main(string[] _args)
  92.     {
  93.         // var _points =
  94.         //     "6,10\n0,14\n9,10\n0,3\n10,4\n4,11\n6,0\n6,12\n4,1\n0,13\n10,12\n3,4\n3,0\n8,4\n1,10\n2,14\n8,10\n9,0"
  95.         //     .Split('\n')
  96.         //     .Select(_s => _s.Split(','))
  97.         //     .Select(_strings => new Point(int.Parse(_strings[0]), int.Parse(_strings[1])))
  98.         //     .ToArray();
  99.  
  100.         var (_points, _instructionSet) = File.ReadAllLines("D:\\Projects\\AoC\\AoC\\AoCDay13.txt")
  101.             .Split(_strings => _strings.ToList().IndexOf(_strings.First(string.IsNullOrWhiteSpace)));
  102.  
  103.         var _puzzlePoints =
  104.             _points
  105.             .Select(_s => _s.Split(','))
  106.             .Select(_strings => new Point(int.Parse(_strings[0]), int.Parse(_strings[1])))
  107.             .ToArray();
  108.  
  109.         var _instructions =
  110.             _instructionSet
  111.             .Where(_s => !string.IsNullOrWhiteSpace(_s))
  112.             .Select(_s => new {_axis = _s.Split('=')[0].Last(), _value = int.Parse(_s.Split('=')[1])});
  113.  
  114.         int _width = _puzzlePoints.Select(_point => _point.X).Concat(new[] {int.MinValue}).Max();
  115.         int _height = _puzzlePoints.Select(_point => _point.Y).Concat(new[] {int.MinValue}).Max();
  116.  
  117.         foreach (var _instruction in _instructions)
  118.         {
  119.             _puzzlePoints = Fold(_puzzlePoints, _instruction._axis, _instruction._value);
  120.         }
  121.  
  122.         Console.WriteLine($"Points Count: {_puzzlePoints.Length}");
  123.         StringBuilder _sb = new StringBuilder();
  124.         Draw(_puzzlePoints, ref _sb, _width, _height);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement