about summary refs log tree commit diff
path: root/Day10.cs
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2019-12-10 01:57:51 -0500
committerBen Harris <ben@tilde.team>2019-12-10 01:57:58 -0500
commit6d22bf07fcb8781ea52d04ca70784aeef6fed5fa (patch)
treecc2bcc982813de656356153c8e8c4c7d8385d43e /Day10.cs
parent663d31564adf3d0714247a091021743ba0876e27 (diff)
day 10 part 1
Diffstat (limited to 'Day10.cs')
-rw-r--r--Day10.cs74
1 files changed, 74 insertions, 0 deletions
diff --git a/Day10.cs b/Day10.cs
new file mode 100644
index 0000000..3050ba4
--- /dev/null
+++ b/Day10.cs
@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+
+namespace aoc2019
+{
+    internal class Day10 : Day
+    {
+        public override int DayNumber => 10;
+
+        private readonly List<Point> asteroids = new List<Point>();
+        private Point best = new Point(-1, -1);
+        private IGrouping<double, Point>[] bestgroups;
+        private int bestcansee;
+
+        public Day10()
+        {
+            var starmap = Input.Select(x => x.Select(y => y == '#').ToArray()).ToArray();
+
+            for (var i = 0; i < starmap.Length; i++)
+                for (var j = 0; j < starmap[i].Length; j++)
+                    if (starmap[i][j])
+                        asteroids.Add(new Point(i, j));
+
+            foreach (var asteroid in asteroids)
+            {
+                var groups = asteroids.Except(new[] { asteroid })
+                    .Select(a => new Point(a.X - asteroid.X, a.Y - asteroid.Y))
+                    .GroupBy(a => Math.Atan2(a.Y, a.X))
+                    .ToArray();
+                var cansee = groups.Count();
+
+                if (cansee > bestcansee)
+                {
+                    best = asteroid;
+                    bestcansee = cansee;
+                    bestgroups = groups;
+                }
+            }
+        }
+
+        public override string Part1()
+        {
+            return $"{bestcansee}";
+        }
+
+        public override string Part2()
+        {
+            var removals = bestgroups
+                .Select(g => new { 
+                    Angle = g.Key, 
+                    Targets = new Queue<Point>(g.OrderBy(a => 
+                        Math.Sqrt(Math.Pow(a.X, 2) + Math.Pow(a.Y, 2))
+                    ))
+                })
+                .OrderBy(g => g.Angle > Math.PI / 2)
+                .ThenByDescending(g => g.Angle);
+
+            var removed = 0;
+            while (true)
+                foreach (var removal in removals)
+                    if (removal.Targets.Count > 0)
+                    {
+                        var toremove = removal.Targets.Dequeue();
+                        removed++;
+                        if (removed == 200)
+                        {
+                            return $"{(toremove.X * 100) + toremove.Y}";
+                        }
+                    }
+        }
+    }
+}