diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index b6700cf..33ebb4c 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -17,6 +17,7 @@ public class Test2015 [DataRow(typeof(Day09), "117", "909")] [DataRow(typeof(Day10), "492982", "6989950")] [DataRow(typeof(Day11), "hepxxyzz", "heqaabcc")] + [DataRow(typeof(Day12), "111754", "")] public void CheckAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); diff --git a/AOC2015/Day12.cs b/AOC2015/Day12.cs index 8e848b5..62d9394 100644 --- a/AOC2015/Day12.cs +++ b/AOC2015/Day12.cs @@ -1,15 +1,49 @@ +using System.Text.Json; + namespace AOC2015; /// /// Day 12: /// -public sealed class Day12 : Day +public sealed partial class Day12 : Day { public Day12() : base(2015, 12, "JSAbacusFramework.io") { } + + [GeneratedRegex(@"-?\d+")] + private static partial Regex Digits(); - public override object Part1() => ""; + public override object Part1() => + Digits().Matches(Input.First()).Sum(n => int.Parse(n.Value)); - public override object Part2() => ""; + public override object Part2() + { + var reader = new Utf8JsonReader(File.ReadAllBytes(FileName)); + var sum = 0; + bool redObject = false, insideObject = false; + + while (reader.Read()) + { + switch (reader.TokenType) + { + case JsonTokenType.StartObject: + insideObject = true; + break; + case JsonTokenType.EndObject: + insideObject = false; + redObject = false; + break; + case JsonTokenType.String: + if (insideObject && reader.GetString()! == "red") + redObject = true; + break; + case JsonTokenType.Number: + if (!redObject) sum += reader.GetInt32(); + break; + } + } + + return sum; + } }