From 2350ac2b0d242ff69a539078edbfff967a6cfde2 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 3 Dec 2018 16:30:50 +0100 Subject: [PATCH] Day02: Add code to pass test --- AdventOfCode2018/Day02.cs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/AdventOfCode2018/Day02.cs b/AdventOfCode2018/Day02.cs index d001502..770b8e1 100644 --- a/AdventOfCode2018/Day02.cs +++ b/AdventOfCode2018/Day02.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace AdventOfCode2018 { @@ -33,9 +34,39 @@ namespace AdventOfCode2018 public class Day02 { + private int CountOccurrencesOfLetter(string text, char letter) + { + return text.Count(c => (c == letter)); + } + + private Tuple HasPairsAndTriplets(string text) + { + bool hasPair = false; + bool hasTriplet = false; + foreach (char c in text) + { + int count = CountOccurrencesOfLetter(text, c); + if (count == 2) { hasPair = true; } + if (count == 3) { hasTriplet = true; } + if (hasPair && hasTriplet) { break; } + } + return new Tuple(hasPair, hasTriplet); + } + public string ResolveDay02(string[] inputs) { - throw new NotImplementedException(); + int pairsCount = 0; + int tripletsCount = 0; + foreach (string input in inputs) + { + var hasPairsAndTriplets = HasPairsAndTriplets(input); + if (hasPairsAndTriplets.Item1) { pairsCount++; } + if (hasPairsAndTriplets.Item2) { tripletsCount++; } + } + + long result = pairsCount * tripletsCount; + + return result.ToString(); } } }