Skip to content

Instantly share code, notes, and snippets.

@csuzw
csuzw / dynamicpredicates.cs
Last active Aug 29, 2015
Dynamic Predicates
View dynamicpredicates.cs
// using DynamicLinq
public static Func<TInput, TOutput> GetFuncFromLinq<TInput, TOutput>(this string predicate)
{
if (string.IsNullOrWhiteSpace(predicate)) return null;
LambdaExpression expression = DynamicExpression.ParseLambda(typeof(TInput), typeof(TOutput), predicate);
Func<TInput, TOutput> func = x => (TOutput)expression.Compile().DynamicInvoke(x);
return func;
@csuzw
csuzw / ExpressionMemberInfo.cs
Last active Aug 29, 2015
MemberInfo for non-Member expressions
View ExpressionMemberInfo.cs
/// <summary>
/// Crude but effective for my requirements.
/// </summary>
public class ExpressionMemberInfo<T, TProperty> : MemberInfo
{
private static readonly Dictionary<ExpressionType, string> _operatorDescriptions = new Dictionary<ExpressionType, string>
{
{ ExpressionType.Add, "+" },
{ ExpressionType.AddAssign, "+" },
{ ExpressionType.AddAssignChecked, "+" },
@csuzw
csuzw / GetKeyPresses.cs
Created Mar 15, 2017
Gets number of key presses required to input message on old phone keypad
View GetKeyPresses.cs
int GetKeyPresses(string message)
{
return (int)message.Select(c => c < 83 ? ((c - 29) % 3) + 1 : Math.Round(Enumerable.Range(0, 8).Zip(new[] { -400129224492.361d, 32421475419.7348d, -1125710194.76048d, 21711311.9794762d, -251208.877408241d, 1743.70653338347d, -6.72322436313127d, 0.011108192375644d }, (a, b) => b * Math.Pow(c, a)).Sum())).Sum();
}
@csuzw
csuzw / DateDiff.cs
Last active Nov 13, 2017
Gets the number of days between 2 dates (at least if they fall between 01/01/0001 and 29/02/4096 (I think!)). Note that this could be even faster if I used such exotic things as conditionals to weed out the easy cases but where would the fun be in that!?!
View DateDiff.cs
public int DateDiff(int y1, int m1, int d1, int y2, int m2, int d2)
{
return GetDays(y2, m2, d2) - GetDays(y1, m1, d1);
}
private int GetDays(int y, int m, int d)
{
var y1 = y - 1;
var m1 = m - 1;
var yleap = y - ((18 - m) >> 4);
@csuzw
csuzw / InverseCaptcha.cs
Last active Dec 3, 2017
Advent of Code 2017 Day 1
View InverseCaptcha.cs
int InverseCaptchPartOne(string input)
{
return DoInverseCaptcha(input, _ => 1);
}
int InverseCaptchPartTwo(string input)
{
return DoInverseCaptcha(input, x => x.Length / 2);
}
@csuzw
csuzw / CorruptionChecksum.cs
Last active Dec 4, 2017
Advent of Code 2017 Day 2
View CorruptionChecksum.cs
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)
@csuzw
csuzw / SpiralMemory.cs
Last active Dec 4, 2017
Advent of Code 2017 Day 3
View SpiralMemory.cs
int SpiralMemoryPartOne(int location)
{
if (location <= 1) return 0;
var ring = 1;
var max = 9;
while (max < location)
{
ring += 1;
max += (((ring + 1) * 2) - 2) * 4;
@csuzw
csuzw / HighEntropyPassphrases.cs
Last active Dec 4, 2017
Advent of Code 2017 Day 4
View HighEntropyPassphrases.cs
int HighEntropyPassphrasesPartOne(string[] passPhrases)
{
return DoHighEntropyPassphrases(passPhrases, w => w);
}
int HighEntropyPassphrasesPartTwo(string[] passPhrases)
{
return DoHighEntropyPassphrases(passPhrases, w => string.Concat(w.OrderBy(c => c)));
}
View keybase.md

Keybase proof

I hereby claim:

  • I am csuzw on github.
  • I am csuzw (https://keybase.io/csuzw) on keybase.
  • I have a public key ASCRAdPs2_BCK9ur7mkddBoPT03Wpqh_eNGaF4cuhcDPDQo

To claim this, I am signing this object:

@csuzw
csuzw / index.js
Created Nov 7, 2019
Azure AD Single Sign On with Cypress
View index.js
// This goes in cypress/plugins/index.js
const AzureAdSingleSignOn = require('./azure-ad-sso/plugin').AzureAdSingleSignOn
module.exports = (on, config) => {
on('task', {AzureAdSingleSignOn:AzureAdSingleSignOn})
}