Skip to content

Commit

Permalink
Day 3 part 2 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
afromogli committed Dec 15, 2019
1 parent a880d09 commit 8b702fe
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
Binary file modified .vs/AdventOfCode/v15/Server/sqlite3/storage.ide-shm
Binary file not shown.
Binary file modified .vs/AdventOfCode/v15/Server/sqlite3/storage.ide-wal
Binary file not shown.
23 changes: 20 additions & 3 deletions Day3/Day3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ static void Main(string[] args)
Console.WriteLine(CalcShortestDistance(test2));
Console.WriteLine(CalcShortestDistance(input1));

Console.WriteLine("Part 2:");

// Part 2
//Console.WriteLine(FindIntersectionWithLeastAmountOfSteps(test1));
Console.WriteLine(FindIntersectionWithLeastAmountOfSteps(input1));

Console.ReadKey();
}
Expand All @@ -29,9 +31,24 @@ private static int FindIntersectionWithLeastAmountOfSteps(string[] wires)
{
List<Wire> allWires = GetWires(wires);
var intersectionPoints = FindIntersections(allWires);

var leastAmountOfSteps = int.MaxValue;

for (int i = 0; i < intersectionPoints.Count; i++)
{
var currIntersectionPoint = intersectionPoints[i];

var targetPoint = currIntersectionPoint.Point;

var stepsToTarget1 = currIntersectionPoint.Wire1.Points.IndexOf(targetPoint);
var stepsToTarget2 = currIntersectionPoint.Wire2.Points.IndexOf(targetPoint);

if (stepsToTarget1 + stepsToTarget2 < leastAmountOfSteps)
{
leastAmountOfSteps = stepsToTarget1 + stepsToTarget2;
}
}

return leastAmountOfSteps;
}

Expand Down Expand Up @@ -73,7 +90,7 @@ private static List<Intersection> FindIntersections(List<Wire> allWires)
}
var otherWire = allWires[j];

var intersectionPoints = currWire.Points.Intersect(otherWire.Points, new PositionComparer()).ToArray();
var intersectionPoints = currWire.Points.Intersect(otherWire.Points).ToArray();

for (int k = 0; k < intersectionPoints.Length; k++)
{
Expand Down
25 changes: 10 additions & 15 deletions Day3/Position.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Day3
namespace Day3
{
public class Position
{
Expand All @@ -18,24 +16,21 @@ public override string ToString()
return $"X:{X}, Y:{Y}";
}


}

public class PositionComparer : IEqualityComparer<Position>
{
public bool Equals(Position item1, Position item2)
public override bool Equals(object obj)
{
if (object.ReferenceEquals(item1, item2))
if (object.ReferenceEquals(this, obj))
return true;
if (item1 == null || item2 == null)
if (obj == null)
return false;
return item1.X.Equals(item2.X) &&
item1.Y.Equals(item2.Y);

var pos = (Position)obj;
return X.Equals(pos.X) &&
Y.Equals(pos.Y);
}

public int GetHashCode(Position item)
public override int GetHashCode()
{
return new { item.X, item.Y }.GetHashCode();
return new { X, Y }.GetHashCode();
}
}
}
5 changes: 5 additions & 0 deletions Day3/Wire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public Wire()
}

public List<Position> Points;

public override string ToString()
{
return $"Points size:{Points.Count}";
}
}
}

0 comments on commit 8b702fe

Please sign in to comment.