From 3826a1324466ce7f8fc4858173b453652695d5a7 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Mon, 9 Dec 2024 00:46:19 -0500 Subject: [PATCH] feat: part 7-2 --- src/AllDays.idr | 1 + src/Day7/Part1.idr | 3 +++ src/Day7/Part2.idr | 21 +++++++++++++++++++++ src/Main.idr | 2 +- 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/Day7/Part2.idr diff --git a/src/AllDays.idr b/src/AllDays.idr index fcb187f..e441f4c 100644 --- a/src/AllDays.idr +++ b/src/AllDays.idr @@ -13,3 +13,4 @@ import public Day5.Part2 import public Day6.Part1 import public Day6.Part2 import public Day7.Part1 +import public Day7.Part2 diff --git a/src/Day7/Part1.idr b/src/Day7/Part1.idr index ad9a0c1..b644d92 100644 --- a/src/Day7/Part1.idr +++ b/src/Day7/Part1.idr @@ -9,6 +9,7 @@ import Utils --- TYPES +public export Op : Type Op = Nat -> Nat -> Nat @@ -21,11 +22,13 @@ parseLine inp = in (,) <$> parseNat part1 <*> (traverse parseNat =<< fromList (words part2)) +export parseInput : String -> Maybe (List (Nat, List1 Nat)) parseInput = traverse parseLine . lines --- DATA +export search : List Op -> (tg, tot : Nat) -> List Nat -> Bool search _ tg tot [] = tg == tot search ops tg tot (x :: xs) = diff --git a/src/Day7/Part2.idr b/src/Day7/Part2.idr new file mode 100644 index 0000000..9cbe7fe --- /dev/null +++ b/src/Day7/Part2.idr @@ -0,0 +1,21 @@ +module Day7.Part2 + +import Data.List1 + +import Day7.Part1 +import Utils + +%default total + +--- DATA + +concat : Nat -> Nat -> Nat +concat x y = cast $ show x ++ show y + +--- SOLUTION + +export +solution : String -> Maybe Nat +solution = map (sum . map fst + . filter (\(tg,x:::xs) => search [(+),(*),concat] tg x xs)) + . parseInput diff --git a/src/Main.idr b/src/Main.idr index 8b60986..65448ab 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 7 Part1 +latest = Pr 7 Part2 solMap : SortedMap Problem (String -> String)