feat: part 7-1
This commit is contained in:
parent
d87cb7d7ee
commit
2196df1811
|
@ -12,3 +12,4 @@ import public Day5.Part1
|
|||
import public Day5.Part2
|
||||
import public Day6.Part1
|
||||
import public Day6.Part2
|
||||
import public Day7.Part1
|
||||
|
|
41
src/Day7/Part1.idr
Normal file
41
src/Day7/Part1.idr
Normal file
|
@ -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
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue