Compare commits

...

4 commits

6 changed files with 78 additions and 3 deletions

View file

@ -14,4 +14,5 @@ executable = advent-of-code-2024
main = Main main = Main
modules = Main, Utils, AllDays, modules = Main, Utils, AllDays,
Data.Problem, Data.Problem,
Day1.Part1, Day1.Part2 Day1.Part1, Day1.Part2,
Day2.Part1, Day2.Part2

View file

@ -2,3 +2,5 @@ module AllDays
import public Day1.Part1 import public Day1.Part1
import public Day1.Part2 import public Day1.Part2
import public Day2.Part1
import public Day2.Part2

View file

@ -20,7 +20,7 @@ export
parseInput : String -> Maybe (List Nat, List Nat) parseInput : String -> Maybe (List Nat, List Nat)
parseInput = map unzip . traverse parseLine . lines parseInput = map unzip . traverse parseLine . lines
--- UTILS --- DATA
distance : Nat -> Nat -> Nat distance : Nat -> Nat -> Nat
distance x y = distance x y =

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

27
src/Day2/Part2.idr Normal file
View file

@ -0,0 +1,27 @@
module Day2.Part2
import Data.List
import Data.List1
import Data.String
import Utils
import Day2.Part1
%default total
--- DATA
reductions : Report -> List Report
reductions (x ::: []) = []
reductions (x ::: [y]) = [y ::: [], x ::: []]
reductions (x ::: (y :: xs)) =
(y ::: xs) :: map (x `cons`) (assert_total $ reductions (y ::: xs))
isSafe' : Report -> Bool
isSafe' rep = isSafe rep || any isSafe (reductions rep)
--- SOLUTION
export
solution : String -> Maybe Nat
solution inp = count isSafe' <$> parseInput inp

View file

@ -1,10 +1,12 @@
module Main module Main
import Data.List1 import Data.List1
import Data.Maybe
import Data.String import Data.String
import Data.SortedMap import Data.SortedMap
import Data.SortedMap.Dependent import Data.SortedMap.Dependent
import Data.Problem import Data.Problem
import Utils
import Language.Reflection import Language.Reflection
@ -16,7 +18,7 @@ import AllDays
||| The latest problem that has been solved. ||| The latest problem that has been solved.
-- NOTE: UPDATE AFTER EACH SOLUTION -- NOTE: UPDATE AFTER EACH SOLUTION
latest : Problem latest : Problem
latest = Pr 1 Part2 latest = Pr 2 Part2
solMap : SortedMap Problem (String -> String) solMap : SortedMap Problem (String -> String)