Skip to content

Instantly share code, notes, and snippets.

@johnboker
Created December 6, 2015 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnboker/628a5b6ad8a0c3423fe1 to your computer and use it in GitHub Desktop.
Save johnboker/628a5b6ad8a0c3423fe1 to your computer and use it in GitHub Desktop.
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