feat: part 1-2
This commit is contained in:
parent
0b415a23cb
commit
1425c6c51d
|
@ -14,5 +14,4 @@ executable = advent-of-code-2024
|
||||||
main = Main
|
main = Main
|
||||||
modules = Main, Utils, AllDays,
|
modules = Main, Utils, AllDays,
|
||||||
Data.Problem,
|
Data.Problem,
|
||||||
Day1.Part1
|
Day1.Part1, Day1.Part2
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
module AllDays
|
module AllDays
|
||||||
|
|
||||||
import public Day1.Part1
|
import public Day1.Part1
|
||||||
|
import public Day1.Part2
|
||||||
|
|
|
@ -16,6 +16,7 @@ parseLine str =
|
||||||
| _ => Nothing
|
| _ => Nothing
|
||||||
in (,) <$> parseNat n1 <*> parseNat n2
|
in (,) <$> parseNat n1 <*> parseNat n2
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
31
src/Day1/Part2.idr
Normal file
31
src/Day1/Part2.idr
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
module Day1.Part2
|
||||||
|
|
||||||
|
import Data.Maybe
|
||||||
|
import Data.SortedMap
|
||||||
|
|
||||||
|
import Day1.Part1
|
||||||
|
|
||||||
|
%default total
|
||||||
|
|
||||||
|
--- DATA
|
||||||
|
|
||||||
|
process : (left, right : List Nat) -> Nat
|
||||||
|
process = go Z empty
|
||||||
|
where
|
||||||
|
go : (out : Nat) -> (cache : SortedMap Nat Nat)
|
||||||
|
-> (left, right : List Nat) -> Nat
|
||||||
|
go out _ [] _ = out
|
||||||
|
go out cache (n :: left) right =
|
||||||
|
let cached = lookup n cache
|
||||||
|
score = case cached of
|
||||||
|
Just c => c
|
||||||
|
Nothing => n * count (==n) right
|
||||||
|
in go (score + out) (if isJust cached
|
||||||
|
then cache
|
||||||
|
else insert n score cache) left right
|
||||||
|
|
||||||
|
--- SOLUTION
|
||||||
|
|
||||||
|
export
|
||||||
|
solution : String -> Maybe Nat
|
||||||
|
solution = map (uncurry process) . parseInput
|
|
@ -1,7 +1,6 @@
|
||||||
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
|
||||||
|
@ -17,7 +16,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 Part1
|
latest = Pr 1 Part2
|
||||||
|
|
||||||
|
|
||||||
solMap : SortedMap Problem (String -> String)
|
solMap : SortedMap Problem (String -> String)
|
||||||
|
|
Loading…
Reference in a new issue