feat: part 2-1
This commit is contained in:
parent
2c9e1baf93
commit
1a0e4a5a04
|
@ -14,4 +14,5 @@ executable = advent-of-code-2024
|
|||
main = Main
|
||||
modules = Main, Utils, AllDays,
|
||||
Data.Problem,
|
||||
Day1.Part1, Day1.Part2
|
||||
Day1.Part1, Day1.Part2,
|
||||
Day2.Part1
|
||||
|
|
|
@ -2,3 +2,4 @@ module AllDays
|
|||
|
||||
import public Day1.Part1
|
||||
import public Day1.Part2
|
||||
import public Day2.Part1
|
||||
|
|
43
src/Day2/Part1.idr
Normal file
43
src/Day2/Part1.idr
Normal file
|
@ -0,0 +1,43 @@
|
|||
module Day2.Part1
|
||||
|
||||
import Data.List
|
||||
import Data.List1
|
||||
import Data.String
|
||||
import Utils
|
||||
|
||||
%default total
|
||||
|
||||
--- TYPES
|
||||
|
||||
public export
|
||||
Report : Type
|
||||
Report = List1 Nat
|
||||
|
||||
--- PARSING
|
||||
|
||||
parseLine : String -> Maybe Report
|
||||
parseLine = traverse parseNat . split (==' ')
|
||||
|
||||
export
|
||||
parseInput : String -> Maybe (List Report)
|
||||
parseInput = traverse parseLine . lines
|
||||
|
||||
--- DATA
|
||||
|
||||
differences : Report -> List Integer
|
||||
differences ns =
|
||||
let is = map natToInteger ns
|
||||
in zipWith (-) (tail is) (forget is)
|
||||
|
||||
export
|
||||
isSafe : Report -> Bool
|
||||
isSafe rep =
|
||||
let diff = differences rep
|
||||
in (all (>0) diff || all (<0) diff)
|
||||
&& all ((<=3) . abs) diff
|
||||
|
||||
--- SOLUTION
|
||||
|
||||
export
|
||||
solution : String -> Maybe Nat
|
||||
solution inp = count isSafe <$> parseInput inp
|
|
@ -16,7 +16,7 @@ import AllDays
|
|||
||| The latest problem that has been solved.
|
||||
-- NOTE: UPDATE AFTER EACH SOLUTION
|
||||
latest : Problem
|
||||
latest = Pr 1 Part2
|
||||
latest = Pr 2 Part1
|
||||
|
||||
|
||||
solMap : SortedMap Problem (String -> String)
|
||||
|
|
Loading…
Reference in a new issue