Skip to content

Advent of Code. Year 2015. Day 5

Published: (2 min read)

--- Day 5: Doesn’t He Have Intern-Elves For This? ---

In today’s puzzle, we need to help Santa figuring out which strings in his text file are naughty or nice.

Table of Contents

Open Table of Contents

Part 1

A nice string is one with all of the following properties:

The easiest way to solve this part is to use regular expressions.

public string Part1(IEnumerable<string> input)
{
  bool HasThreeVowels(string str) =>
    Regex.Matches(str, @"[aeiou]").Count >= 3;

  bool HasDoubledLetter(string str) =>
    Regex.IsMatch(str, @"(\w)\1");

  bool ContainsNaughtyStrings(string str) =>
    Regex.IsMatch(str, @"ab|cd|pq|xy");

  bool IsNiceString(string str) =>
    HasThreeVowels(str)
    && HasDoubledLetter(str)
    && !ContainsNaughtyStrings(str);

  var niceStrings = input.Count(IsNiceString);
  return niceStrings.ToString();
}

Part 2

In the second part we have new properties:

The solution is very much like the first part, except now we have two methods and different regular expressions.

public string Part2(IEnumerable<string> input)
{
  bool HasPair(string str) =>
    Regex.IsMatch(str, @"(\w{2}).*\1");

  bool HasDuplicate(string str) =>
    Regex.IsMatch(str, @"(\w).\1");

  bool IsNiceString(string str) =>
    HasDuplicate(str)
    && HasPair(str);

  var niceStrings = input.Count(IsNiceString);
  return niceStrings.ToString();
}

Links:

Other posts