fix day 10

This commit is contained in:
Ben Harris 2019-12-11 23:47:22 -05:00
parent db981bdfef
commit ddb972d58f
2 changed files with 47 additions and 41 deletions

View File

@ -1,6 +1,6 @@
using aoc2019.lib;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Linq; using System.Linq;
namespace aoc2019 namespace aoc2019
@ -9,66 +9,65 @@ namespace aoc2019
{ {
public override int DayNumber => 10; public override int DayNumber => 10;
private readonly List<Point> asteroids = new List<Point>(); private readonly HashSet<(int x, int y)> asteroids = new HashSet<(int x, int y)>();
private Point best = new Point(-1, -1); private (int x, int y) best = (x: -1, y: -1);
private IGrouping<double, Point>[] bestgroups;
private int bestcansee; private int bestcansee;
public Day10() public Day10()
{ {
var starmap = Input.Select(x => x.Select(y => y == '#').ToArray()).ToArray(); asteroids = Input
.Select((r, y) => r.Select((c, x) => (x, y, isAsteroid: c == '#')).ToArray())
for (var i = 0; i < starmap.Length; i++) .SelectMany(r => r)
for (var j = 0; j < starmap[i].Length; j++) .Where(a => a.isAsteroid)
if (starmap[i][j]) .Select(a => (a.x, a.y))
asteroids.Add(new Point(i, j)); .ToHashSet();
}
public override string Part1()
{
foreach (var asteroid in asteroids) foreach (var asteroid in asteroids)
{ {
var groups = asteroids.Except(new[] { asteroid }) var cansee = asteroids
.Select(a => new Point(a.X - asteroid.X, a.Y - asteroid.Y)) .Except(new[] { asteroid })
.GroupBy(a => Math.Atan2(a.Y, a.X)) .Select(a => (x: a.x - asteroid.x, y: a.y - asteroid.y))
.ToArray(); .GroupBy(a => Math.Atan2(a.y, a.x))
var cansee = groups.Count(); .Count();
if (cansee > bestcansee) if (cansee > bestcansee)
{ {
best = asteroid; best = asteroid;
bestcansee = cansee; bestcansee = cansee;
bestgroups = groups;
} }
} }
}
public override string Part1()
{
return $"{bestcansee}"; return $"{bestcansee}";
} }
public override string Part2() public override string Part2()
{ {
var removals = bestgroups static IEnumerable<(int x, int y, double angle, double dist)> GetValue(Queue<(int x, int y, double angle, double dist)> q)
.Select(g => new { {
Angle = g.Key, if (q.Count > 0) yield return q.Dequeue();
Targets = new Queue<Point>(g.OrderBy(a => }
Math.Sqrt(Math.Pow(a.X - best.X, 2) + Math.Pow(a.Y - best.Y, 2))
))
})
.OrderBy(g => g.Angle > Math.PI / 2)
.ThenByDescending(g => g.Angle);
var removed = 0; return asteroids
while (true) .Where(a => a != best)
foreach (var removal in removals) .Select(a =>
if (removal.Targets.Count > 0) {
{ var xdist = a.x - best.x;
var toremove = removal.Targets.Dequeue(); var ydist = a.y - best.y;
removed++; var angle = Math.Atan2(xdist, ydist);
if (removed == 200) return (a.x, a.y, angle, dist: Math.Sqrt(xdist * xdist + ydist * ydist));
{ })
return $"{(toremove.X * 100) + toremove.Y}"; .ToLookup(a => a.angle)
} .OrderByDescending(a => a.Key)
} .Select(a => new Queue<(int x, int y, double angle, double dist)>(a.OrderBy(b => b.dist)))
.Repeat()
.SelectMany(GetValue)
.Skip(199)
.Take(1)
.Select(a => a.x * 100 + a.y)
.Single()
.ToString();
} }
} }
} }

View File

@ -22,5 +22,12 @@ namespace aoc2019.lib
{ {
return string.Join(delimiter, enumerable); return string.Join(delimiter, enumerable);
} }
public static IEnumerable<T> Repeat<T>(this IEnumerable<T> sequence, int? count = null)
{
while (count == null || count-- > 0)
foreach (var item in sequence)
yield return item;
}
} }
} }