2022-12-05 03:04:48 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
2022-12-04 20:05:16 +00:00
|
|
|
|
2022-12-03 05:55:49 +00:00
|
|
|
namespace AOC2015;
|
2022-11-11 18:42:42 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-12-03 05:41:38 +00:00
|
|
|
/// Day 12: <a href="https://adventofcode.com/2015/day/12"/>
|
2022-11-11 18:42:42 +00:00
|
|
|
/// </summary>
|
2023-09-20 18:38:58 +00:00
|
|
|
public sealed partial class Day12() : Day(2015, 12, "JSAbacusFramework.io")
|
2022-11-11 18:42:42 +00:00
|
|
|
{
|
2022-12-04 20:05:16 +00:00
|
|
|
[GeneratedRegex(@"-?\d+")]
|
|
|
|
private static partial Regex Digits();
|
|
|
|
|
|
|
|
public override object Part1() =>
|
|
|
|
Digits().Matches(Input.First()).Sum(n => int.Parse(n.Value));
|
2022-11-11 18:42:42 +00:00
|
|
|
|
2022-12-04 20:05:16 +00:00
|
|
|
public override object Part2()
|
|
|
|
{
|
2022-12-05 03:04:48 +00:00
|
|
|
dynamic j = JsonConvert.DeserializeObject(Input.First())!;
|
|
|
|
return GetSum(j, "red");
|
2022-12-04 20:05:16 +00:00
|
|
|
}
|
2022-12-05 03:04:48 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2023-12-01 18:21:24 +00:00
|
|
|
private static long GetSum(JValue val, string avoid) => val.Type == JTokenType.Integer ? (long)(val.Value ?? 0L) : 0;
|
|
|
|
}
|