couldn't get it to work with Utf8JsonReader
pulled out the trusty newtonsoft.json
This commit is contained in:
parent
20c93249bb
commit
048868fc4b
|
@ -17,7 +17,7 @@ public class Test2015
|
|||
[DataRow(typeof(Day09), "117", "909")]
|
||||
[DataRow(typeof(Day10), "492982", "6989950")]
|
||||
[DataRow(typeof(Day11), "hepxxyzz", "heqaabcc")]
|
||||
[DataRow(typeof(Day12), "111754", "")]
|
||||
[DataRow(typeof(Day12), "111754", "65402")]
|
||||
public void CheckAllDays(Type dayType, string part1, string part2)
|
||||
{
|
||||
Common.CheckDay(dayType, part1, part2);
|
||||
|
|
|
@ -26,4 +26,8 @@
|
|||
<ProjectReference Include="..\AOC.Common\AOC.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Text.Json;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace AOC2015;
|
||||
|
||||
|
@ -10,7 +11,7 @@ public sealed partial class Day12 : Day
|
|||
public Day12() : base(2015, 12, "JSAbacusFramework.io")
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
[GeneratedRegex(@"-?\d+")]
|
||||
private static partial Regex Digits();
|
||||
|
||||
|
@ -19,31 +20,20 @@ public sealed partial class Day12 : Day
|
|||
|
||||
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;
|
||||
dynamic j = JsonConvert.DeserializeObject(Input.First())!;
|
||||
return GetSum(j, "red");
|
||||
}
|
||||
}
|
||||
|
||||
private long GetSum(JObject o, string? avoid = null)
|
||||
{
|
||||
var shouldAvoid = o.Properties()
|
||||
.Select(a => a.Value).OfType<JValue>()
|
||||
.Select(v => v.Value).Contains(avoid);
|
||||
|
||||
return shouldAvoid ? 0 : o.Properties().Sum((dynamic a) => (long)GetSum(a.Value, avoid));
|
||||
}
|
||||
|
||||
private long GetSum(JArray arr, string avoid) => arr.Sum((dynamic a) => (long)GetSum(a, avoid));
|
||||
|
||||
private long GetSum(JValue val, string avoid) => val.Type == JTokenType.Integer ? (long)(val.Value ?? 0L) : 0;
|
||||
}
|
Loading…
Reference in New Issue