Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCodeDay5
{
class MainClass
{
public static void Main (string[] args)
{
StreamReader ss = new StreamReader ("../../input.txt");
string line;
var input = new List<string> ();
while((line = ss.ReadLine()) != null)
{
input.Add (line);
}
var nice1 = input
.Where (a => a.Sum (c => (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ? 1 : 0) >= 3)
.Where (b => b.Select ((c, i) => b.Substring (i).TakeWhile (x => x == c).Count ()).Max () > 1)
.Where (d => !(d.Contains ("ab") || d.Contains ("cd") || d.Contains ("pq") || d.Contains ("xy")))
.ToList ();
var nice2 = input
.Where (a => ContainsDoublePair(a))
.Where (a=> ContainsSeparatedDouble(a))
.ToList ();
foreach(var s in nice2)
{
Console.WriteLine(s);
}
Console.WriteLine ($"Part 1 Nice Strings: {nice1.Count()}");
Console.WriteLine ($"Part 2 Nice Strings: {nice2.Count()}");
}
public static bool ContainsDoublePair(string s)
{
var pairs = new List<Tuple<string, int>>();
for (int i = 0; i < s.Length - 1; i++) {
var pair = s.Substring (i, 2);
pairs.Add (new Tuple<string, int>(pair, i));
}
var doublePairs = pairs.Where (a => pairs.Sum(b=>b.Item1 == a.Item1 ? 1 : 0) > 1);
if (doublePairs.Count () > 0) {
var min = doublePairs.Min (a => a.Item2);
var max = doublePairs.Max (a => a.Item2);
return (max - min) > 1;
}
return false;
}
public static bool ContainsSeparatedDouble(string s)
{
for (int i = 0; i < s.Length - 2; i++) {
if (s [i] == s [i + 2])
return true;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.