From 6d22bf07fcb8781ea52d04ca70784aeef6fed5fa Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Tue, 10 Dec 2019 01:57:51 -0500 Subject: [PATCH] day 10 part 1 --- Day10.cs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ Program.cs | 3 +- aoc2019.csproj | 3 ++ input/day10.in | 24 ++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 Day10.cs create mode 100644 input/day10.in 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 asteroids = new List(); + private Point best = new Point(-1, -1); + private IGrouping[] 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(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}"; + } + } + } + } +} diff --git a/Program.cs b/Program.cs index 2b98b6d..53e4814 100644 --- a/Program.cs +++ b/Program.cs @@ -31,6 +31,7 @@ namespace aoc2019 private static IEnumerable GetDays() => Assembly.GetExecutingAssembly().GetTypes() .Where(t => t.BaseType == typeof(Day)) - .Select(t => (Day)Activator.CreateInstance(t)); + .Select(t => (Day)Activator.CreateInstance(t)) + .OrderBy(d => d.DayNumber); } } diff --git a/aoc2019.csproj b/aoc2019.csproj index 3a0fd0a..eca0950 100644 --- a/aoc2019.csproj +++ b/aoc2019.csproj @@ -33,6 +33,9 @@ PreserveNewest + + PreserveNewest + diff --git a/input/day10.in b/input/day10.in new file mode 100644 index 0000000..bf4ecf6 --- /dev/null +++ b/input/day10.in @@ -0,0 +1,24 @@ +##.##..#.####...#.#.#### +##.###..##.#######..##.. +..######.###.#.##.###### +.#######.####.##.#.###.# +..#...##.#.....#####..## +#..###.#...#..###.#..#.. +###..#.##.####.#..##..## +.##.##....###.#..#....#. +########..#####..####### +##..#..##.#..##.#.#.#..# +##.#.##.######.#####.... +###.##...#.##...#.###### +###...##.####..##..##### +##.#...#.#.....######.## +.#...####..####.##...##. +#.#########..###..#.#### +#.##..###.#.######.##### +##..##.##...####.#...##. +###...###.##.####.#.##.. +####.#.....###..#.####.# +##.####..##.#.##..##.#.# +#####..#...####..##..#.# +.##.##.##...###.##...### +..###.########.#.###..#. \ No newline at end of file