Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class AoCDay13
- {
- /// <summary>
- /// Fold along the x axis given the y coordinate.
- /// </summary>
- /// <param name="_points"></param>
- /// <param name="_y"></param>
- /// <param name="_height"></param>
- public static Point[] FoldX(Point[] _points, int _y)
- {
- HashSet<Point> _pointsList = new HashSet<Point>();
- foreach (var _point in _points)
- {
- if (_point.Y > _y)
- {
- Point _p = new Point(_point.X, _y + -(_point.Y - _y));
- _pointsList.Add(_p);
- }
- else
- {
- _pointsList.Add(new Point(_point.X, _point.Y));
- }
- }
- return _pointsList.ToArray();
- }
- /// <summary>
- /// Fold along the x axis given the y coordinate.
- /// </summary>
- /// <param name="_points"></param>
- /// <param name="_y"></param>
- /// <param name="_height"></param>
- public static Point[] FoldY(Point[] _points, int _x)
- {
- HashSet<Point> _pointsList = new HashSet<Point>();
- foreach (var _point in _points)
- {
- if (_point.X > _x)
- {
- Point _p = new Point(_x + -(_point.X - _x), _point.Y);
- _pointsList.Add(_p);
- }
- else
- {
- _pointsList.Add(new Point(_point.X, _point.Y));
- }
- }
- return _pointsList.ToArray();
- }
- public static Point[] Fold(Point[] _points, char _axis, int _value)
- {
- switch (_axis)
- {
- case 'x':
- return FoldY(_points, _value);
- break;
- case 'y':
- return FoldX(_points, _value);
- default:
- return _points;
- }
- }
- public static void Draw(Point[] _points, ref StringBuilder _sb, int _width, int _height)
- {
- for (int _y = 0; _y < _height / 10; _y++)
- {
- for (int _x = 0; _x < _width / 10; _x++)
- {
- Point _point = new Point(_x, _y);
- if (_points.Contains(_point))
- Console.Write("#");
- else
- {
- Console.Write(" ");
- }
- // So many new lines makes the text shoot up too quick so delay it.
- // Plus, it looks cool =D
- Thread.Sleep(15);
- }
- Console.WriteLine("");
- }
- }
- public static (T[] _first, T[] _second) Split<T>(this T[] array, Func<T[], int> _func) {
- return (array.Take(_func(array)).ToArray(), array.Skip(_func(array)).ToArray());
- }
- public static void Main(string[] _args)
- {
- // var _points =
- // "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"
- // .Split('\n')
- // .Select(_s => _s.Split(','))
- // .Select(_strings => new Point(int.Parse(_strings[0]), int.Parse(_strings[1])))
- // .ToArray();
- var (_points, _instructionSet) = File.ReadAllLines("D:\\Projects\\AoC\\AoC\\AoCDay13.txt")
- .Split(_strings => _strings.ToList().IndexOf(_strings.First(string.IsNullOrWhiteSpace)));
- var _puzzlePoints =
- _points
- .Select(_s => _s.Split(','))
- .Select(_strings => new Point(int.Parse(_strings[0]), int.Parse(_strings[1])))
- .ToArray();
- var _instructions =
- _instructionSet
- .Where(_s => !string.IsNullOrWhiteSpace(_s))
- .Select(_s => new {_axis = _s.Split('=')[0].Last(), _value = int.Parse(_s.Split('=')[1])});
- int _width = _puzzlePoints.Select(_point => _point.X).Concat(new[] {int.MinValue}).Max();
- int _height = _puzzlePoints.Select(_point => _point.Y).Concat(new[] {int.MinValue}).Max();
- foreach (var _instruction in _instructions)
- {
- _puzzlePoints = Fold(_puzzlePoints, _instruction._axis, _instruction._value);
- }
- Console.WriteLine($"Points Count: {_puzzlePoints.Length}");
- StringBuilder _sb = new StringBuilder();
- Draw(_puzzlePoints, ref _sb, _width, _height);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement