From c632ab023dec68d9206f8a7dd1e18b73ef99b315 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Mon, 27 Jan 2025 23:00:02 -0500 Subject: [PATCH] Year 2015 Day 12 Part 2 --- src/Years/Y2015/Day12.md | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Years/Y2015/Day12.md b/src/Years/Y2015/Day12.md index 92ce6dc..2829698 100644 --- a/src/Years/Y2015/Day12.md +++ b/src/Years/Y2015/Day12.md @@ -72,9 +72,32 @@ sumNumbers dbl type value = dFoldL sumNumbers' dbl value sumNumbers' dbl _ value = dbl ``` +Filter out objects containing a "red" key + +```idris +noRed : (type : JSONType) -> (value : JSONValue type) -> Bool +noRed TObject value = + let (types ** vals) = getValues value + in case dFind (\t, v => + case t of + TString => v == (VString "red") + _ => False + ) vals of + Nothing => True + Just _ => False +noRed _ value = True + +sumNumbersNoRed : + Double -> (type : JSONType) -> (value : JSONValue type) -> Double +sumNumbersNoRed dbl type value = + case dFilter noRed value of + Nothing => dbl + Just value => sumNumbers dbl type value +``` + ## Part Functions -### Day 1 +### Part 1 Parse our JSONs, then fold our `sumNumbers` reducer over them. @@ -88,10 +111,20 @@ part1 = do pure (result, (types ** values)) ``` +### Part 2 + +```idris +part2 : (types : List JSONType ** DList JSONType JSONValue types) + -> Eff (PartEff String) Double +part2 (types ** values) = do + let result = dFoldL sumNumbersNoRed 0.0 values + pure result +``` + ```idris hide public export day12 : Day -day12 = First 12 part1 +day12 = Both 12 part1 part2 ``` ## References