mirror of
https://github.com/parnic/advent-of-code-2022.git
synced 2025-06-16 13:40:13 -05:00
10x speedup on part 1
Mostly from not testing and adding each individual vector, just the x component we care about. Some of the improvement came from setting capacity on the hash set up front. I could get even better if I was smarter about computing ranges of numbers rather than this dumb approach of putting each number into a hashset to function as a union/intersection surrogate.
This commit is contained in:
35
src/15.cs
35
src/15.cs
@ -1,6 +1,7 @@
|
|||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using aoc2022.Util;
|
using aoc2022.Util;
|
||||||
|
using Math = System.Math;
|
||||||
|
|
||||||
namespace aoc2022;
|
namespace aoc2022;
|
||||||
|
|
||||||
@ -44,45 +45,25 @@ internal class Day15 : Day
|
|||||||
internal override string Part1()
|
internal override string Part1()
|
||||||
{
|
{
|
||||||
int interestedY = 2000000;
|
int interestedY = 2000000;
|
||||||
HashSet<ivec2> emptySpace = new();
|
HashSet<long> emptySpace = new(10_000_000);
|
||||||
for (int i = 0; i < knownSensors.Count; i++)
|
for (int i = 0; i < knownSensors.Count; i++)
|
||||||
{
|
{
|
||||||
var s = knownSensors[i];
|
var s = knownSensors[i];
|
||||||
var b = knownBeacons[i];
|
var b = knownBeacons[i];
|
||||||
var dist = s.ManhattanDistanceTo(b);
|
var dist = s.ManhattanDistanceTo(b);
|
||||||
|
|
||||||
for (int j = 0; ; j++)
|
var yDist = Math.Abs(interestedY - s.y);
|
||||||
|
var xWidth = dist - yDist;
|
||||||
|
if (xWidth >= 0)
|
||||||
{
|
{
|
||||||
var testVec = new ivec2(s.x + j, interestedY);
|
for (long j = s.x - xWidth; j <= s.x + xWidth; j++)
|
||||||
if (testVec.ManhattanDistanceTo(s) <= dist)
|
|
||||||
{
|
{
|
||||||
if (!knownBeacons.Contains(testVec))
|
emptySpace.Add(j);
|
||||||
{
|
|
||||||
emptySpace.Add(testVec);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int j = 0; ; j++)
|
|
||||||
{
|
|
||||||
var testVec = new ivec2(s.x - j, interestedY);
|
|
||||||
if (testVec.ManhattanDistanceTo(s) <= dist)
|
|
||||||
{
|
|
||||||
if (!knownBeacons.Contains(testVec))
|
|
||||||
{
|
|
||||||
emptySpace.Add(testVec);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
knownBeacons.Where(b => b.y == interestedY).ForEach(b => emptySpace.Remove(b.x));
|
||||||
return $"Empty spaces at y={interestedY:N0}: <+white>{emptySpace.Count}";
|
return $"Empty spaces at y={interestedY:N0}: <+white>{emptySpace.Count}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user