Frigging bug when determaning if an selected item is a number, completed day 3 p2

This commit is contained in:
Rob Stoffelen 2023-12-12 10:05:14 +01:00
parent 25522f06f6
commit 2fa4a61525
4 changed files with 21 additions and 79 deletions

View File

@ -7,9 +7,9 @@ InputReader inputReader = new()
//IsDebug = true
};
//inputReader.SetInputByChallange(5);
inputReader.SetInputByChallange(3);
IChallange challange = new Day11(inputReader);
IChallange challange = new Day03(inputReader);
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");

View File

@ -19,27 +19,15 @@ namespace AdventOfCode.Solutions._2023
public int Length => Section.Length;
public bool IsDigit() => Section.Length > 1;
public bool IsDigit() => Section.All(char.IsDigit);
public bool IsPartSymbol() => !IsDigit();
public List<int> XRange => Enumerable.Range(X, Length).ToList();
public List<int> XRangeWithSpace => Enumerable.Range(X-1, Length+2).ToList();
public List<int> YRange => Enumerable.Range(Y - 1, 3).ToList();
public override string ToString()
{
return $"[{Y},{X}] {Section}";
}
}
public async Task<string> GetSolutionPart1()
{
List<PartNumber> parts = [];
int row = 0;
int total = 0;
await foreach (string line in _inputReader.ReadAsStringLine())
{
row++;
@ -49,73 +37,28 @@ namespace AdventOfCode.Solutions._2023
List<Rectangle> numbers = parts.Where(p => p.IsDigit()).Select(number => new Rectangle(new Point(number.X - 1, number.Y - 1), new Point(number.X + number.Length, number.Y + 1), number.Section)).ToList();
List<Point> symbols = parts.Where(p => p.IsPartSymbol()).Select(symbol => new Point(symbol.X, symbol.Y, symbol.Section)).ToList();
return numbers.Where(number => symbols.Any(s => number.Intersect(s))).Select(num => int.Parse(num.Value)).Sum().ToString();
//foreach(PartNumber number in numbers)
//{
// var yfilter = symbols.Where(symbol => number.YRange.Contains(symbol.Y));
// var xfilter = yfilter.Where(symbol => number.XRangeWithSpace.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;
//
//await foreach(string line in _inputReader.ReadAsStringLine())
//{
// MatchCollection matchCollection = FindDigits().Matches(line);
//
// foreach (Match match in matchCollection.Cast<Match>())
// {
// var section = grid.GetSection(match.Index - 1, row - 1, match.Index + match.Length, row + 1).ToList();
// bool isPartNumber = section.Any(n => !(n.Char == '.' || (n.Char >= '0' && n.Char <= '9')));
// if (isPartNumber) {
// total += int.Parse(match.Value);
// }
// }
//
// row++;
//}
return total.ToString();
}
public async Task<string> GetSolutionPart2()
{
List<PartNumber> parts = [];
int row = 0;
int total = 0;
await foreach (string line in _inputReader.ReadAsStringLine())
{
MatchCollection matchCollection = FindPartItems().Matches(line);
parts.AddRange(matchCollection.Select(match => new PartNumber { X = match.Index, Y = row, Section = match.Value }));
row++;
MatchCollection matchCollection = FindPartItems().Matches(line);
parts.AddRange(matchCollection.Select(match => new PartNumber { X = match.Index + 1, Y = row, Section = match.Value }));
}
List<PartNumber> numbers = parts.Where(p => p.IsDigit()).ToList();
List<PartNumber> gears = parts.Where(p => p.IsPartSymbol() && p.Section == "*").ToList();
List<Rectangle> numbers = parts.Where(p => p.IsDigit()).Select(number => new Rectangle(new Point(number.X - 1, number.Y - 1), new Point(number.X + number.Length, number.Y + 1), number.Section)).ToList();
List<Point> gears = parts.Where(p => p.IsPartSymbol() && p.Section == "*").Select(symbol => new Point(symbol.X, symbol.Y, symbol.Section)).ToList();
foreach (PartNumber gear in gears)
{
// check if there are 2 numbers around
//List<PartNumber> ratios = GetSection(numbers, gear).ToList();
//if (ratios.Count != 2)
// continue;
//int totalRatio = int.Parse(ratios[0].Section) * int.Parse(ratios[1].Section);
//total += totalRatio;
}
return total.ToString();
return gears.Select(g => numbers.Where(n => n.Intersect(g)))
.Where(n => n.Count() == 2)
.Select(num => int.Parse(num.First().Value) * int.Parse(num.Last().Value))
.Sum()
.ToString();
}
[GeneratedRegex("(\\d+)", RegexOptions.Compiled)]

View File

@ -12,6 +12,9 @@
</ItemGroup>
<ItemGroup>
<Compile Update="2023\Day 12\Day12.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2023\Day 11\Day11.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>

View File

@ -1,10 +1,6 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1