Last active
December 4, 2017 09:19
-
-
Save csuzw/8d9b04b3d32765d40b2f659c267f00ba to your computer and use it in GitHub Desktop.
Advent of Code 2017 Day 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int CorruptionChecksumPartOne(int[][] input) | |
{ | |
return input.Sum(r => | |
{ | |
(var max, var min) = r.Aggregate((max: int.MinValue, min: int.MaxValue), (acc, i) => (i > acc.max ? i : acc.max, i < acc.min ? i : acc.min)); | |
return max - min; | |
}); | |
} | |
int CorruptionChecksumPartTwo(int[][] input) | |
{ | |
return input.Sum(r => | |
{ | |
(var hi, var lo) = r.Pairs().First(i => (i.hi % i.lo) == 0); | |
return hi / lo; | |
}); | |
} | |
public static class Extensions | |
{ | |
public static IEnumerable<(int hi, int lo)> Pairs(this int[] array) | |
{ | |
foreach (var i in array) | |
{ | |
foreach (var j in array) | |
{ | |
if (i > j) yield return (i, j); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment