Completed day 13, worked in day 2 p 2

This commit is contained in:
Rob 2023-12-13 22:04:22 +01:00
parent 154ab4a0ca
commit 392224af82
10 changed files with 2692 additions and 11 deletions

View File

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

View File

@ -85,5 +85,27 @@ namespace AdventOfCode.Core
return result;
}
public async IAsyncEnumerable<Grid<T>> ReadToGrids<T>() where T : Node, new()
{
Grid<T> result = new();
int row = 0;
string[] data = await ReadAsArrayString();
foreach (string line in data)
{
if (string.IsNullOrWhiteSpace(line))
{
yield return result;
result = new Grid<T>();
row = 0;
continue;
}
// create the nodes from the lines
result.DataGrid.AddRange(line.Select((c, i) => new T { X = i, Y = row, Char = c }));
row++;
}
yield return result;
yield break;
}
}
}

View File

@ -4,8 +4,8 @@
{
public List<T> DataGrid { get; set; } = [];
public int Columns => DataGrid.Max(n => n.X);
public int Rows => DataGrid.Max(n => n.Y);
public int Columns => DataGrid.Max(n => n.X) + 1;
public int Rows => DataGrid.Max(n => n.Y) + 1;
public Grid() { }
@ -14,7 +14,9 @@
public Grid(IEnumerable<T> data) : this(data.ToArray()) { }
public IEnumerable<T> GetRow(int rowNumber) => DataGrid.Where(n => n.X == rowNumber);
public string GetRowAsString(int columnNumber) => string.Concat(DataGrid.Where(n => n.Y == columnNumber).OrderBy(n => n.X).Select(n => n.Char));
public IEnumerable<T> GetColumn(int columnNumber) => DataGrid.Where(n => n.Y == columnNumber);
public string GetColumnAsString(int rowNumber) => string.Concat(DataGrid.Where(n => n.X == rowNumber).OrderBy(n => n.Y).Select(n => n.Char));
public T GetNode(int x, int y) => DataGrid.First(n => n.X == x && n.Y == y);
public IEnumerable<T> FindWithValue(char toFind) => DataGrid.Where(n => n.Char == toFind);

View File

@ -0,0 +1,139 @@
using AdventOfCode.Core;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
public partial class Day12(InputReader reader) : IChallange
{
private InputReader _inputReader = reader;
public async Task<string> GetSolutionPart1()
{
long total = 0;
await foreach(string line in _inputReader.ReadAsStringLine())
{
string[] splitted = line.Split(' ');
int[] groups = splitted[1].Split(',').Select(int.Parse).ToArray();
total += Possibilities(splitted[0], groups);
}
return total.ToString();
}
public async Task<string> GetSolutionPart2()
{
long total = 0;
await foreach (string line in _inputReader.ReadAsStringLine())
{
string[] splitted = line.Split(' ');
var joinedLine = string.Join("?", new[] { splitted[0], splitted[0], splitted[0], splitted[0], splitted[0] });
int[] groups = splitted[1].Split(',').Select(int.Parse).ToArray();
int[] groupsTotal = groups.Concat(groups).Concat(groups).Concat(groups).Concat(groups).ToArray();
total += Possibilities2(joinedLine, groupsTotal, 0, 0, 0);
}
return total.ToString();
}
private long Possibilities(string lineToValidate, int[] groups)
{
long total = 0;
if (lineToValidate.Contains('?'))
{
var regex = new Regex(Regex.Escape("?"));
total += Possibilities(regex.Replace(lineToValidate, ".", 1), groups);
total += Possibilities(regex.Replace(lineToValidate, "#", 1), groups);
return total;
}
MatchCollection matches = GroupMatch().Matches(lineToValidate);
if (matches.Count == groups.Length &&
groups.Where((g, i) => matches[i].Length == g).Count() == groups.Length)
{
//Console.WriteLine($"{lineToValidate} is valid");
return 1;
}
else
return 0;
}
private long Possibilities2(string lineToValidate, int[] groups, int readIndex, int groupIndex, int captureSize)
{
// if group index is 0 there are no captures and not captures ruynning
if (groupIndex > 0)
{
int[] splitGroups = lineToValidate.Split('.', StringSplitOptions.RemoveEmptyEntries).Select(s => s.Length).ToArray();
// something to validate!
if (readIndex < lineToValidate.Length && // not reached the end
lineToValidate[readIndex] == '.' && // possible end of capture
captureSize > 0) // in capture mode but with a . caputure has ended
{
for (int i = 0; i < groupIndex; i++)
{
if (splitGroups[i] != groups[i])
{
Console.WriteLine($"{lineToValidate} is invalid");
return 0; // group lengths do not match, reject
}
}
// valid so far
// reset the capture size
groupIndex++;
captureSize = 0;
}
}
if (!lineToValidate.Contains('?')) // no more variations
{
int[] splitGroups = lineToValidate.Split('.', StringSplitOptions.RemoveEmptyEntries).Select(s => s.Length).ToArray();
for (int i = 0; i < groups.Length; i++)
{
if (splitGroups[i] != groups[i])
{
Console.WriteLine($"{lineToValidate} is invalid");
return 0; // group lengths do not match, reject
}
}
// not invalid combos so valid
return 1;
}
//if (lineToValidate[readIndex] is '.' )
long total = 0;
//go search for # or ?
for (int charIndex = readIndex; charIndex < lineToValidate.Length; charIndex++)
{
if (lineToValidate[charIndex] == '.')
{
readIndex++;
continue;
}
if (lineToValidate[charIndex] == '#')
{
total += Possibilities2(lineToValidate, groups, readIndex + 1, groupIndex + 1, captureSize + 1);
break;
}
if (lineToValidate[charIndex] == '?')
{
var regex = new Regex(Regex.Escape("?"));
total += Possibilities2(regex.Replace(lineToValidate, ".", 1), groups, readIndex + 1, groupIndex, captureSize);
total += Possibilities2(regex.Replace(lineToValidate, "#", 1), groups, readIndex + 1, groupIndex, captureSize + 1);
return total;
}
}
}
[GeneratedRegex("#+")]
private static partial Regex GroupMatch();
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,94 @@
using AdventOfCode.Core;
using AdventOfCode.Solutions._2023.Day_13;
using System.Data;
namespace AdventOfCode.Solutions._2023
{
public class Day13(InputReader reader) : IChallange
{
private InputReader _inputReader = reader;
public async Task<string> GetSolutionPart1()
{
int rowMirrors = 0, columnMirrors = 0;
await foreach (Grid<Node> map in _inputReader.ReadToGrids<Node>())
{
(List<string> rows, List<string> columns) = map.GetRowsAndColumns();
int r = 0, c = 0;
r = GetReflectedRow(rows, false);
rowMirrors += r;
if (r == 0)
{
c = GetReflectedRow(columns, false);
columnMirrors += c;
}
}
return (rowMirrors * 100 + columnMirrors).ToString();
}
public async Task<string> GetSolutionPart2()
{
int rowMirrors = 0, columnMirrors = 0;
await foreach (Grid<Node> map in _inputReader.ReadToGrids<Node>())
{
(List<string> rows, List<string> columns) = map.GetRowsAndColumns();
int r = 0, c = 0;
r = GetReflectedRow(rows, true);
rowMirrors += r;
if (r == 0)
{
c = GetReflectedRow(columns, true);
columnMirrors += c;
}
}
return (rowMirrors * 100 + columnMirrors).ToString();
}
private static int GetReflectedRow(List<string> lines, bool derectSmudge = false)
{
for (var rowIndex = 0; rowIndex < lines.Count - 1; rowIndex++)
{
bool foundMirrors = false, foundSmudge = false;
for (var charIndex = 0; charIndex < lines[rowIndex].Length; charIndex++)
{
for (var smudge = 0; smudge <= Math.Min(rowIndex, lines.Count - 2 - rowIndex); smudge++)
{
if (lines[rowIndex - smudge][charIndex] != lines[rowIndex + 1 + smudge][charIndex])
{
if (derectSmudge && !foundSmudge)
{
foundSmudge = true;
}
else
{
foundMirrors = true;
break;
}
}
}
if (foundMirrors)
{
break;
}
}
if (derectSmudge && !foundSmudge)
{
// continue to find the damn thing
continue;
}
if (!foundMirrors)
{
return rowIndex + 1;
}
}
return 0;
}
}
}

View File

@ -0,0 +1,22 @@
namespace AdventOfCode.Solutions._2023.Day_13
{
internal static class GridMirrorExtention
{
public static (List<string> Rows, List<string> Columns) GetRowsAndColumns(this Grid<Node> grid)
{
List<string> rows = [], columns = [];
for (int rowIndex = 0; rowIndex < grid.Rows; rowIndex++)
{
rows.Add(grid.GetRowAsString(rowIndex));
}
for (int columnIndex = 0; columnIndex < grid.Columns; columnIndex++)
{
columns.Add(grid.GetColumnAsString(columnIndex));
}
return (rows, columns);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,12 @@
</ItemGroup>
<ItemGroup>
<Compile Update="2023\Day 13\Day13.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2023\Day 00\Day00.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2023\Day 12\Day12.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
@ -33,7 +39,7 @@
<Compile Update="2023\Day 06\Day06.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2023\Day 00\Day00.cs">
<Compile Update="2023\Day 12\Day12.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2023\Day 05\Day05.cs">

View File

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