From 2196df1811ea7d3a3a953c41555416fe0026ebe3 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Mon, 9 Dec 2024 00:34:47 -0500 Subject: [PATCH] feat: part 7-1 --- src/AllDays.idr | 1 + src/Day7/Part1.idr | 41 +++++++++++++++++++++++++++++++++++++++++ src/Main.idr | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/Day7/Part1.idr diff --git a/src/AllDays.idr b/src/AllDays.idr index 8d9e3f0..fcb187f 100644 --- a/src/AllDays.idr +++ b/src/AllDays.idr @@ -12,3 +12,4 @@ import public Day5.Part1 import public Day5.Part2 import public Day6.Part1 import public Day6.Part2 +import public Day7.Part1 diff --git a/src/Day7/Part1.idr b/src/Day7/Part1.idr new file mode 100644 index 0000000..211a540 --- /dev/null +++ b/src/Day7/Part1.idr @@ -0,0 +1,41 @@ +module Day7.Part1 + +import Data.List +import Data.List1 +import Data.String +import Data.Vect + +import Utils + +%default total + +--- TYPES + +Op : Type +Op = Nat -> Nat -> Nat + +--- PARSING + +parseLine : String -> Maybe (Nat, List1 Nat) +parseLine inp = + let (part1 ::: [part2]) = split (==':') inp + | _ => Nothing + in (,) <$> parseNat part1 + <*> (traverse parseNat =<< fromList (words part2)) + +parseInput : String -> Maybe (List (Nat, List1 Nat)) +parseInput = traverse parseLine . lines + +--- DATA + +search : List Op -> Nat -> List1 Nat -> Bool +search _ n (x ::: []) = x == n +search ops n (x ::: y :: xs) = + -- heuristic: abandon if running total is larger than target + x <= n && any (\op => assert_total $ search ops n (op x y ::: xs)) ops + +--- SOLUTION + +export +solution : String -> Maybe Nat +solution = map (sum . map fst . filter (uncurry $ search [(+),(*)])) . parseInput diff --git a/src/Main.idr b/src/Main.idr index ce6eeb1..8b60986 100644 --- a/src/Main.idr +++ b/src/Main.idr @@ -13,7 +13,7 @@ import AllDays ||| The latest problem that has been solved. -- NOTE: UPDATE AFTER EACH SOLUTION latest : Problem -latest = Pr 6 Part2 +latest = Pr 7 Part1 solMap : SortedMap Problem (String -> String)