diff --git a/AdvendOfCode.Runner/Program.cs b/AdvendOfCode.Runner/Program.cs index 149d813..a332657 100644 --- a/AdvendOfCode.Runner/Program.cs +++ b/AdvendOfCode.Runner/Program.cs @@ -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()}"); diff --git a/AdventOfCode.Solutions/2023/Day 03/Day03.cs b/AdventOfCode.Solutions/2023/Day 03/Day03.cs index 88aa503..a9bf1c3 100644 --- a/AdventOfCode.Solutions/2023/Day 03/Day03.cs +++ b/AdventOfCode.Solutions/2023/Day 03/Day03.cs @@ -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 XRange => Enumerable.Range(X, Length).ToList(); - - public List XRangeWithSpace => Enumerable.Range(X-1, Length+2).ToList(); - - public List YRange => Enumerable.Range(Y - 1, 3).ToList(); - - public override string ToString() - { - return $"[{Y},{X}] {Section}"; - } } public async Task GetSolutionPart1() { List parts = []; int row = 0; - int total = 0; await foreach (string line in _inputReader.ReadAsStringLine()) { row++; @@ -49,73 +37,28 @@ namespace AdventOfCode.Solutions._2023 List 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 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 grid = await _inputReader.ReadToGrid(); - //int row = 0; - // - //await foreach(string line in _inputReader.ReadAsStringLine()) - //{ - // MatchCollection matchCollection = FindDigits().Matches(line); - // - // foreach (Match match in matchCollection.Cast()) - // { - // 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 GetSolutionPart2() { List 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 numbers = parts.Where(p => p.IsDigit()).ToList(); - List gears = parts.Where(p => p.IsPartSymbol() && p.Section == "*").ToList(); + List 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 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 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)] diff --git a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj index abf309d..a83b1d1 100644 --- a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj +++ b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj @@ -12,6 +12,9 @@ + + Always + Always diff --git a/AdventOfCode.Solutions/day-00-input.txt b/AdventOfCode.Solutions/day-00-input.txt index a0bda53..c5bec3a 100644 --- a/AdventOfCode.Solutions/day-00-input.txt +++ b/AdventOfCode.Solutions/day-00-input.txt @@ -1,10 +1,6 @@ -...#...... -.......#.. -#......... -.......... -......#... -.#........ -.........# -.......... -.......#.. -#...#..... \ No newline at end of file +???.### 1,1,3 +.??..??...?##. 1,1,3 +?#?#?#?#?#?#?#? 1,3,1,6 +????.#...#... 4,1,1 +????.######..#####. 1,6,5 +?###???????? 3,2,1 \ No newline at end of file