From e941e22c83baffe21fa8f969f5b7cdef32f2b996 Mon Sep 17 00:00:00 2001 From: Rob Date: Tue, 19 Dec 2023 21:37:19 +0100 Subject: [PATCH] completed day 19 p1 --- AdvendOfCode.Runner/Program.cs | 2 +- AdventOfCode.Solutions/2023/Day 18/Day18.cs | 11 +- AdventOfCode.Solutions/2023/Day 19/Day19.cs | 134 +++++++++++++++----- 3 files changed, 114 insertions(+), 33 deletions(-) diff --git a/AdvendOfCode.Runner/Program.cs b/AdvendOfCode.Runner/Program.cs index 7dd748a..48c91e7 100644 --- a/AdvendOfCode.Runner/Program.cs +++ b/AdvendOfCode.Runner/Program.cs @@ -4,7 +4,7 @@ using AdventOfCode.Core; InputReader inputReader = new() { - IsDebug = true + //IsDebug = true }; //inputReader.SetInputByChallange(12); diff --git a/AdventOfCode.Solutions/2023/Day 18/Day18.cs b/AdventOfCode.Solutions/2023/Day 18/Day18.cs index 5626a28..8807195 100644 --- a/AdventOfCode.Solutions/2023/Day 18/Day18.cs +++ b/AdventOfCode.Solutions/2023/Day 18/Day18.cs @@ -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 = '.'; } diff --git a/AdventOfCode.Solutions/2023/Day 19/Day19.cs b/AdventOfCode.Solutions/2023/Day 19/Day19.cs index 3416417..267425e 100644 --- a/AdventOfCode.Solutions/2023/Day 19/Day19.cs +++ b/AdventOfCode.Solutions/2023/Day 19/Day19.cs @@ -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 GetSolutionPart1() { - return string.Empty; + List instructions = []; + List 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 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 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 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 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; } } \ No newline at end of file