changed day 3 code
This commit is contained in:
parent
13340e6319
commit
6b46d0d39c
@ -4,12 +4,12 @@ using AdventOfCode.Core;
|
||||
|
||||
InputReader inputReader = new()
|
||||
{
|
||||
//IsDebug = true
|
||||
IsDebug = true
|
||||
};
|
||||
|
||||
inputReader.SetInputByChallange(2);
|
||||
inputReader.SetInputByChallange(3);
|
||||
|
||||
IChallange challange = new Day02(inputReader);
|
||||
IChallange challange = new Day03(inputReader);
|
||||
|
||||
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
|
||||
|
||||
|
||||
@ -7,12 +7,6 @@ namespace AdventOfCode.Solutions._2023
|
||||
{
|
||||
private InputReader _inputReader = reader;
|
||||
|
||||
private struct Point
|
||||
{
|
||||
public int X { get; set; }
|
||||
public int Y { get; set; }
|
||||
}
|
||||
|
||||
private class PartNumber
|
||||
{
|
||||
public int X { get; set; }
|
||||
@ -27,6 +21,10 @@ namespace AdventOfCode.Solutions._2023
|
||||
|
||||
public bool IsPartSymbol() => !IsDigit();
|
||||
|
||||
public List<int> XRange => Enumerable.Range(X, Length).ToList();
|
||||
|
||||
public List<int> YRange => Enumerable.Range(Y - 1, 3).ToList();
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{Y},{X}] {Section}";
|
||||
@ -48,11 +46,18 @@ namespace AdventOfCode.Solutions._2023
|
||||
List<PartNumber> numbers = parts.Where(p => p.IsDigit()).ToList();
|
||||
List<PartNumber> symbols = parts.Where(p => p.IsPartSymbol()).ToList();
|
||||
|
||||
var intersected = numbers.Where(number => IsPointInAnyBox(symbols, number)).ToList();
|
||||
|
||||
//var intersected = numbers.Where(number => GetSection(symbols, number.X - 1, number.Y - 1, number.X + number.Length, number.Y + 1).Any()).ToList();
|
||||
|
||||
total = numbers.Where(number => GetSection(symbols, number.X - 1, number.Y - 1, number.X + number.Length, number.Y + 1).Any()).Sum(number => int.Parse(number.Section));
|
||||
foreach(PartNumber number in numbers)
|
||||
{
|
||||
var yfilter = symbols.Where(symbol => number.YRange.Contains(symbol.Y));
|
||||
var xfilter = yfilter.Where(symbol => number.XRange.Intersect(symbol.XRange).Any());
|
||||
bool isPartNumber = xfilter.Any();
|
||||
if(isPartNumber)
|
||||
{
|
||||
Console.WriteLine("Adding " + number.ToString());
|
||||
total += int.Parse(number.Section);
|
||||
Console.WriteLine("Total: " + total.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
//Grid<Node> grid = await _inputReader.ReadToGrid<Node>();
|
||||
//int row = 0;
|
||||
@ -94,67 +99,19 @@ namespace AdventOfCode.Solutions._2023
|
||||
foreach (PartNumber gear in gears)
|
||||
{
|
||||
// check if there are 2 numbers around
|
||||
List<PartNumber> ratios = GetSection(numbers, gear).ToList();
|
||||
//List<PartNumber> ratios = GetSection(numbers, gear).ToList();
|
||||
|
||||
if (ratios.Count != 2)
|
||||
continue;
|
||||
//if (ratios.Count != 2)
|
||||
// continue;
|
||||
|
||||
int totalRatio = int.Parse(ratios[0].Section) * int.Parse(ratios[1].Section);
|
||||
//int totalRatio = int.Parse(ratios[0].Section) * int.Parse(ratios[1].Section);
|
||||
|
||||
total += totalRatio;
|
||||
//total += totalRatio;
|
||||
}
|
||||
|
||||
return total.ToString();
|
||||
}
|
||||
|
||||
private IEnumerable<PartNumber> GetSection(List<PartNumber> toSearch, int fromX, int fromY, int toX, int toY)
|
||||
{
|
||||
return toSearch.Where(node => node.X >= fromX && node.X + node.Length <= toX && node.Y >= fromY && node.Y <= toY);
|
||||
}
|
||||
|
||||
private bool IsPointInAnyBox(List<PartNumber> targets, PartNumber source)
|
||||
{
|
||||
return targets.Any(target => IsPointInBox(target, source));
|
||||
}
|
||||
|
||||
private bool IsPointInBox(PartNumber target, PartNumber source)
|
||||
{
|
||||
Point targetA = new Point { X = target.X, Y = target.Y }, targetB = new Point { X = target.X + target.Length - 1, Y = target.Y };
|
||||
Point sourceA = new Point { X = source.X, Y = source.Y };
|
||||
return IsPointInBox(targetA, targetB, sourceA);
|
||||
}
|
||||
|
||||
private bool DoBoxesOverlap(List<PartNumber> targets, PartNumber source)
|
||||
{
|
||||
return targets.Any(target => DoBoxesOverlap(target, source));
|
||||
}
|
||||
|
||||
private bool DoBoxesOverlap(PartNumber target, PartNumber source)
|
||||
{
|
||||
Point targetA = new Point { X = target.X, Y = target.Y }, targetB = new Point { X = target.X + target.Length - 1, Y = target.Y };
|
||||
Point sourceA = new Point { X = source.X - 1, Y = source.Y - 1 }, sourceB = new Point { X = source.X + source.Length, Y = source.Y + 1 };
|
||||
return IsPointInBox(targetA, targetB, sourceA) && IsPointInBox(targetA, targetB, sourceB);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private bool IsPointInBox(Point boxPointA, Point boxPointB, Point pointSource)
|
||||
{
|
||||
return pointSource.X >= boxPointA.X &&
|
||||
pointSource.X <= boxPointB.X &&
|
||||
pointSource.Y >= boxPointA.Y &&
|
||||
pointSource.Y <= boxPointB.Y;
|
||||
}
|
||||
|
||||
private IEnumerable<PartNumber> GetSection(List<PartNumber> toSearch, PartNumber source)
|
||||
{
|
||||
return toSearch.Where(target =>
|
||||
target.X >= source.X - 1 &&
|
||||
target.X + target.Length <= source.X &&
|
||||
target.Y >= source.Y - 1 &&
|
||||
target.Y <= source.Y + 1);
|
||||
}
|
||||
|
||||
[GeneratedRegex("(\\d+)", RegexOptions.Compiled)]
|
||||
private static partial Regex FindDigits();
|
||||
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
|
||||
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
|
||||
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
|
||||
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
|
||||
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
|
||||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
||||
Loading…
Reference in New Issue
Block a user