From ba91b53e54ffb562683624955144ea6130517c8a Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 20 Sep 2023 11:34:49 -0400 Subject: [PATCH] 2015 day 16 --- AOC.Test/Test2015.cs | 1 + AOC2015/Day16.cs | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index b8b5dfe..a730cf5 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -21,6 +21,7 @@ public class Test2015 [DataRow(typeof(Day13), "733", "725")] [DataRow(typeof(Day14), "2655", "1059")] [DataRow(typeof(Day15), "222870", "117936")] + [DataRow(typeof(Day16), "103", "405")] public void CheckAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); diff --git a/AOC2015/Day16.cs b/AOC2015/Day16.cs index 2e54214..d361bf9 100644 --- a/AOC2015/Day16.cs +++ b/AOC2015/Day16.cs @@ -13,7 +13,43 @@ public sealed class Day16 : Day { } - public override object Part1() => ""; + private IEnumerable Common() + { + return Input + .Select(i => Regex.Replace(i, @": \d\d", ": 9")) + .WhereMatch("children: 3") + .WhereMatch("samoyeds: 2") + .WhereMatch("akitas: 0") + .WhereMatch("vizslas: 0") + .WhereMatch("cars: 2") + .WhereMatch("perfumes: 1"); + } - public override object Part2() => ""; + public override object Part1() + { + return Common() + .WhereMatch("cats: 7") + .WhereMatch("trees: 3") + .WhereMatch("pomeranians: 3") + .WhereMatch("goldfish: 5") + .Single() + .Split(' ', ':')[1]; + } + + public override object Part2() + { + return Common() + .WhereMatch("cats: [89]") + .WhereMatch("trees: [4-9]") + .WhereMatch("pomeranians: [012]") + .WhereMatch("goldfish: [0-4]") + .Single() + .Split(' ', ':')[1]; + } } + +public static class Day16Extensions +{ + public static IEnumerable WhereMatch(this IEnumerable input, string pattern) => + input.Where(i => !i.Contains(pattern.Split(' ')[0]) || Regex.IsMatch(i, pattern)); +} \ No newline at end of file