diff options
Diffstat (limited to 'Day04.cs')
-rw-r--r-- | Day04.cs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Day04.cs b/Day04.cs new file mode 100644 index 0000000..f4ddcf9 --- /dev/null +++ b/Day04.cs @@ -0,0 +1,49 @@ +using System.Linq; + +namespace aoc2019 +{ + internal sealed class Day04 : Day + { + private readonly int end; + + private readonly int start; + + public Day04() : base(4, "Secure Container") + { + var range = Input.First().Split('-').Select(int.Parse).ToList(); + start = range[0]; + end = range[1]; + } + + private bool IsValid(int i) + { + var prev = 0; + var hasDup = false; + foreach (var c in i.ToString()) + { + var curr = c - '0'; + if (curr < prev) return false; + if (curr == prev) hasDup = true; + prev = curr; + } + + return i >= start && i <= end && hasDup; + } + + private bool HasOnePair(int i) + { + var s = i.ToString(); + return IsValid(i) && s.Select(c => s.Count(j => j == c)).Any(c => c == 2); + } + + protected override string Part1() + { + return $"{Enumerable.Range(start, end).Count(IsValid)}"; + } + + protected override string Part2() + { + return $"{Enumerable.Range(start, end).Count(HasOnePair)}"; + } + } +} \ No newline at end of file |