diff --git a/AOC.Common/Extensions.cs b/AOC.Common/Extensions.cs index f285db8..5ab9f2d 100644 --- a/AOC.Common/Extensions.cs +++ b/AOC.Common/Extensions.cs @@ -122,4 +122,10 @@ public static class Extensions public static IEnumerable> WhereValue( this IEnumerable> source, Func 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); + } } \ No newline at end of file diff --git a/AOC.Test/Test2018.cs b/AOC.Test/Test2018.cs index e9ff1ad..dbf7e4e 100644 --- a/AOC.Test/Test2018.cs +++ b/AOC.Test/Test2018.cs @@ -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); diff --git a/AOC2018/Day01.cs b/AOC2018/Day01.cs index d33edad..b0981d1 100644 --- a/AOC2018/Day01.cs +++ b/AOC2018/Day01.cs @@ -5,7 +5,7 @@ namespace AOC2018; /// 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(); + var freq = 0; + return Input.Select(int.Parse).Repeat().Any(f => !frequencies.Add(freq += f)) ? freq : 0; + } } diff --git a/AOC2018/Day02.cs b/AOC2018/Day02.cs index 07b33f0..319c7b1 100644 --- a/AOC2018/Day02.cs +++ b/AOC2018/Day02.cs @@ -5,15 +5,34 @@ namespace AOC2018; /// 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"); + } +} \ No newline at end of file