feat: part 2-1

This commit is contained in:
Kiana Sheibani 2024-12-03 04:04:36 -05:00
parent 2c9e1baf93
commit 1a0e4a5a04
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
4 changed files with 47 additions and 2 deletions

View file

@ -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

View file

@ -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
View 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

View file

@ -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)