-
-
Save robhol/57db55426ee48332647ebf9ed3f15235 to your computer and use it in GitHub Desktop.
This file contains 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 Answer1(string input) => Parse(input).Sum(r => r.Max() - r.Min()); | |
int Answer2(string input) => Parse(input).Sum(r => | |
r.CartesianProduct() | |
.Where(t => t.a != t.b && t.a % t.b == 0) | |
.Select(t => t.a / t.b) | |
.Single()); | |
IEnumerable<IEnumerable<int>> Parse(string input) => | |
input.Split("\r\n") | |
.Select(x => | |
x.Split("\t") | |
.Select(int.Parse)); | |
static class ext | |
{ | |
public static IEnumerable<(T a, T b)> CartesianProduct<T>(this IEnumerable<T> ts) | |
{ | |
var tsa = ts.ToArray(); | |
foreach(var a in tsa) | |
foreach(var b in tsa) | |
yield return (a, b); | |
} | |
public static string[] Split(this string str, string delimiter, StringSplitOptions options = StringSplitOptions.None) => | |
str.Split(new[]{delimiter}, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment