Cleared day 3
This commit is contained in:
parent
2fa4a61525
commit
154ab4a0ca
@ -1,6 +1,5 @@
|
||||
using AdventOfCode.Core;
|
||||
using AdventOfCode.Core.Shared.Grid;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AdventOfCode.Solutions._2023
|
||||
@ -26,21 +25,27 @@ namespace AdventOfCode.Solutions._2023
|
||||
|
||||
public async Task<string> GetSolutionPart1()
|
||||
{
|
||||
List<PartNumber> parts = [];
|
||||
int row = 0;
|
||||
await foreach (string line in _inputReader.ReadAsStringLine())
|
||||
{
|
||||
row++;
|
||||
MatchCollection matchCollection = FindPartItems().Matches(line);
|
||||
parts.AddRange(matchCollection.Select(match => new PartNumber { X = match.Index + 1, Y = row, Section = match.Value }));
|
||||
}
|
||||
|
||||
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();
|
||||
var (Numbers, Symbols) = await GetNumbersAndSymbols();
|
||||
return Numbers
|
||||
.Where(number => Symbols.Any(s => number.Intersect(s)))
|
||||
.Select(num => int.Parse(num.Value))
|
||||
.Sum()
|
||||
.ToString();
|
||||
}
|
||||
|
||||
public async Task<string> GetSolutionPart2()
|
||||
{
|
||||
var (Numbers, Symbols) = await GetNumbersAndSymbols();
|
||||
return Symbols
|
||||
.Where(s => s.Value == "*")
|
||||
.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();
|
||||
}
|
||||
|
||||
private async Task<(List<Rectangle> Numbers, List<Point> Symbols)> GetNumbersAndSymbols()
|
||||
{
|
||||
List<PartNumber> parts = [];
|
||||
int row = 0;
|
||||
@ -52,13 +57,9 @@ 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> gears = parts.Where(p => p.IsPartSymbol() && p.Section == "*").Select(symbol => new Point(symbol.X, symbol.Y, symbol.Section)).ToList();
|
||||
List<Point> symbols = parts.Where(p => p.IsPartSymbol()).Select(symbol => new Point(symbol.X, symbol.Y, symbol.Section)).ToList();
|
||||
|
||||
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();
|
||||
return (numbers, symbols);
|
||||
}
|
||||
|
||||
[GeneratedRegex("(\\d+)", RegexOptions.Compiled)]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user