From 596c74d0d0af8bbd0a5e0e4f33c944df14ef7b9e Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Wed, 7 Jun 2023 23:58:57 +0100 Subject: [PATCH] [2022] Day6 added --- AdventOfCode/Program.cs | 1 + AdventOfCode/README.md | 2 ++ AdventOfCode/Year2022/Day6.cs | 27 +++++++++++++++++++++++++++ README.md | 2 ++ 4 files changed, 32 insertions(+) create mode 100644 AdventOfCode/Year2022/Day6.cs diff --git a/AdventOfCode/Program.cs b/AdventOfCode/Program.cs index 411452e..5117423 100644 --- a/AdventOfCode/Program.cs +++ b/AdventOfCode/Program.cs @@ -10,6 +10,7 @@ class Program new Day2(); new Day3(); new Day4(); + new Day6(); new Day7(); } } \ No newline at end of file diff --git a/AdventOfCode/README.md b/AdventOfCode/README.md index 1cf062a..a61e2c4 100644 --- a/AdventOfCode/README.md +++ b/AdventOfCode/README.md @@ -6,4 +6,6 @@ * [Day 1](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day1.cs) * [Day 2](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day2.cs) * [Day 3](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day3.cs) +* [Day 4](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day4.cs) +* [Day 6](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day6.cs) * [Day 7](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day7.cs) \ No newline at end of file diff --git a/AdventOfCode/Year2022/Day6.cs b/AdventOfCode/Year2022/Day6.cs new file mode 100644 index 0000000..99b02dd --- /dev/null +++ b/AdventOfCode/Year2022/Day6.cs @@ -0,0 +1,27 @@ +namespace AdventOfCode.Year2022; + +public class Day6 +{ + private static readonly string _input = File.ReadAllText("inputs/day6.txt"); + + public Day6() + { + Console.WriteLine("Day6 Solution"); + Console.WriteLine($"Part1 Result: {GetValidMarkerIndex(4)}"); + Console.WriteLine($"Part2 Result: {GetValidMarkerIndex(14)}"); + Console.WriteLine("\n=============================\n"); + } + + private static int? GetValidMarkerIndex(int size) + { + for (int i = 0; i < _input.Length-size; i++) + if (ValidateMarker(_input.Substring(i, size))) + return i + size; + return null; + } + + private static bool ValidateMarker(string marker) + { + return !marker.Where((c1, i) => marker.Where((c2, j) => c1 == c2 && i != j).Any()).Any(); + } +} \ No newline at end of file diff --git a/README.md b/README.md index 8846701..108bcab 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,6 @@ * [Day 1](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day1.cs) * [Day 2](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day2.cs) * [Day 3](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day3.cs) +* [Day 4](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day4.cs) +* [Day 6](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day6.cs) * [Day 7](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day7.cs) \ No newline at end of file