completed day 19 p1

This commit is contained in:
Rob 2023-12-19 21:37:19 +01:00
parent 22a31d0b29
commit e941e22c83
3 changed files with 114 additions and 33 deletions

View File

@ -4,7 +4,7 @@ using AdventOfCode.Core;
InputReader inputReader = new()
{
IsDebug = true
//IsDebug = true
};
//inputReader.SetInputByChallange(12);

View File

@ -29,12 +29,15 @@
int total = set.Count;
int movedOver = 0;
char prev = '.';
for(int row = 0; row <= set.Max(d => d.Y); row++)
//Console
for(long row = set.Min(d => d.Y); row <= set.Max(d => d.Y); row++)
{
for(int column = 0; column <= set.Max(d => d.X); column++)
for(long column = set.Min(d => d.X); column <= set.Max(d => d.X); column++)
{
if (!set.Add(new(column, row)))
{
//Console.Write('#');
if (prev != '#')
movedOver++;
@ -42,13 +45,15 @@
continue;
}
//Console.Write('.');
if (movedOver % 2 == 1)
{
prev = '.';
total++;
}
}
//Console.WriteLine();
movedOver = 0;
prev = '.';
}

View File

@ -1,4 +1,7 @@
namespace AdventOfCode.Solutions._2023
using System.Reflection.Metadata.Ecma335;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
public class Day19(InputReader reader) : IChallange
{
@ -6,7 +9,53 @@
public async Task<string> GetSolutionPart1()
{
return string.Empty;
List<InstructionSet> instructions = [];
List<Part> parts = [];
bool instructionsDone = false;
await foreach(string line in _inputReader.ReadAsStringLine())
{
if (string.IsNullOrWhiteSpace(line))
{
instructionsDone = true;
continue;
}
if (!instructionsDone)
{
instructions.Add(ParseLine(line));
}
else
{
int[] values = Regex.Matches(line,@"\d+")
.SelectMany(match => match.Captures.Select(cap => cap.Value))
.Select(int.Parse)
.ToArray();
parts.Add(new(values[0], values[1], values[2], values[3]));
}
}
List<Part> acceptedPart = [];
foreach(Part part in parts)
{
InstructionSet set = instructions.First(set => set.Name == "in");
var processResult = string.Empty;
while (true)
{
processResult = set.Process(part);
if (processResult.Length == 1)
break;
set = instructions.First(set => set.Name == processResult);
}
if (processResult == "A")
{
acceptedPart.Add(part);
}
}
return acceptedPart.Sum(p => p.Sum).ToString();
}
public async Task<string> GetSolutionPart2()
@ -14,41 +63,50 @@
return string.Empty;
}
}
internal static InstructionSet ParseLine(string line)
{
string[] nameSplit = line.Split('{');
// build the instructions
Instruction instruction = new();
for (int instructionIndex = 0; instructionIndex < nameSplit[1].Length - 1; instructionIndex++)
internal static InstructionSet ParseLine(string line)
{
// first char is the Category
instruction.Category = nameSplit[1][instructionIndex++];
// second char is the operator
instruction.Operator = nameSplit[1][instructionIndex++];
// from third untill ':' is the compair value
string comp = nameSplit[1].Substring(instructionIndex, nameSplit[1].IndexOf(':'));
instruction.CompairValue = int.Parse(comp);
instructionIndex += comp.Length + 1; // skip the :
// if true value
instruction.resultTrue = nameSplit[1].Substring(instructionIndex, nameSplit[1].IndexOf(','));
instructionIndex = instruction.resultTrue.Length + 1; // skip the ,
// if false value
// check if the instructionIndex + 1 is an operator
if (nameSplit[1][instructionIndex + 1] == '>' || nameSplit[1][instructionIndex + 1] == '<')
{
string[] nameSplit = line.Split('{');
// build the instructions
List<Instruction> instructions = [];
foreach (string instructionText in nameSplit[1][..^1].Split(','))
{
if (!instructionText.Any(c => c ==':'))
{
instructions.Add(new() { ValueTrue = instructionText });
continue;
}
Instruction instruction = new()
{
Category = instructionText[0],
Operator = instructionText[1],
CompairValue = int.Parse(instructionText[2..instructionText.IndexOf(':')]),
ValueTrue = instructionText[(instructionText.IndexOf(':') + 1)..]
};
instructions.Add(instruction);
}
return new() { Name = nameSplit[0], Instructions = instructions };
}
}
internal class InstructionSet
{
public string Name { get; set; }
public List<Instruction> Instructions { get; set; }
public string Process(Part part)
{
for(int instructionIndex = 0; instructionIndex < Instructions.Count - 1; instructionIndex++)
{
if (Instructions[instructionIndex].Matches(part))
return Instructions[instructionIndex].ValueTrue;
}
//public string Process
return Instructions[^1].ValueTrue;
}
}
internal class Instruction
@ -56,7 +114,25 @@
public char Category;
public char Operator;
public int CompairValue;
public string resultTrue;
public string resultFalse;
public string ValueTrue;
public bool Matches(Part part)
{
int value = 0;
switch(Category)
{
case 'x': value = part.x; break;
case 'm': value = part.m; break;
case 'a': value = part.a; break;
case 's': value = part.s; break;
}
return Operator == '>' ? value > CompairValue : value < CompairValue;
}
}
public record Part(int x, int m, int a, int s)
{
public int Sum => x + m + a + s;
}
}