ben
/
aoc
1
0
Fork 0

2018 day 1 and 2
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2022-12-10 20:18:34 -05:00
parent 5102c48e6d
commit 77bfe5a8d3
4 changed files with 40 additions and 9 deletions

View File

@ -122,4 +122,10 @@ public static class Extensions
public static IEnumerable<KeyValuePair<TKey, TValue>> WhereValue<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> source, Func<TValue, bool> func) =>
source.Where(pair => func(pair.Value));
public static int HammingDistance(this string s1, string other)
{
if (s1.Length != other.Length) throw new("Strings must be equal length.");
return s1.Zip(other).Count(s => s.First != s.Second);
}
}

View File

@ -6,7 +6,8 @@ namespace AOC.Test;
public class Test2018
{
[DataTestMethod]
[DataRow(typeof(Day01), "", "")]
[DataRow(typeof(Day01), "582", "488")]
[DataRow(typeof(Day02), "5166", "cypueihajytordkgzxfqplbwn")]
public void CheckAllDays(Type dayType, string part1, string part2)
{
Common.CheckDay(dayType, part1, part2);

View File

@ -5,7 +5,7 @@ namespace AOC2018;
/// </summary>
public sealed class Day01 : Day
{
public Day01() : base(2018, 1, "Puzzle Name")
public Day01() : base(2018, 1, "Chronal Calibration")
{
}
@ -13,7 +13,12 @@ public sealed class Day01 : Day
{
}
public override object Part1() => "";
public override object Part1() => Input.Select(int.Parse).Sum();
public override object Part2() => "";
public override object Part2()
{
var frequencies = new HashSet<int>();
var freq = 0;
return Input.Select(int.Parse).Repeat().Any(f => !frequencies.Add(freq += f)) ? freq : 0;
}
}

View File

@ -5,15 +5,34 @@ namespace AOC2018;
/// </summary>
public sealed class Day02 : Day
{
public Day02() : base(2018, 2, "Puzzle Name")
public Day02() : base(2018, 2, "Inventory Management System")
{
}
public override void ProcessInput()
{
}
public override object Part1() => "";
private static bool HasNChars(string line, int count)
{
for (var i = 'a'; i <= 'z'; i++)
if (line.Count(c => c == i) == count)
return true;
public override object Part2() => "";
}
return false;
}
public override object Part1() =>
Input.Count(l => HasNChars(l, 2)) * Input.Count(l => HasNChars(l, 3));
public override object Part2()
{
var input = Input.ToImmutableList();
foreach (var id1 in input)
foreach (var id2 in input.Where(line2 => id1.HammingDistance(line2) == 1))
return id1.Remove(id1.Zip(id2).Indexed().First(i => i.Value.First != i.Value.Second).Key, 1);
throw new("Correct IDs not found");
}
}