diff options
author | Ben Harris <ben@tilde.team> | 2019-12-04 11:32:43 -0500 |
---|---|---|
committer | Ben Harris <ben@tilde.team> | 2019-12-04 11:32:43 -0500 |
commit | 184c3ede8bddb620e7da07341a72ac692a8925ab (patch) | |
tree | 23a85cbd0bf2eeea2fdfb0625222c8c6e092b8ae /Day4.cs | |
parent | 06316af729215c25240d831608b6b5ff2d02fb8b (diff) |
day 4
Diffstat (limited to 'Day4.cs')
-rw-r--r-- | Day4.cs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/Day4.cs b/Day4.cs new file mode 100644 index 0000000..8e57b00 --- /dev/null +++ b/Day4.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace aoc2019 +{ + internal class Day4 : Day + { + int start, end; + + public Day4() + { + var range = File.ReadLines("input/day4.in") + .First() + .Split('-') + .Select(i => int.Parse(i)) + .ToList(); + + start = range[0]; end = range[1]; + } + + private bool IsValid(int i) + { + return Math.Floor(Math.Log10(i) + 1) == 6 + && i >= start + && i <= end + && IsIncreasingDigits(i); + } + + private bool IsIncreasingDigits(int i) + { + int prev = 0; + bool hasDup = false; + foreach (var c in i.ToString()) + { + int curr = c - '0'; + if (curr < prev) + return false; + if (curr == prev) + hasDup = true; + prev = curr; + } + return hasDup; + } + + private bool Part2Criterion(int i) + { + var s = i.ToString(); + return s.Select(c => s.Count(j => j == c)).Any(c => c == 2); + } + + public override void Part1() + { + Console.WriteLine(Enumerable.Range(start, end).Count(i => IsValid(i))); + } + + public override void Part2() + { + Console.WriteLine(Enumerable.Range(start,end).Count(i => IsValid(i) && Part2Criterion(i))); + } + } +} |