This repository was archived by the owner on Nov 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDay02.hs
66 lines (55 loc) · 1.75 KB
/
Day02.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- |
-- Module : AOC.Challenge.Day02
-- License : BSD3
--
-- Stability : experimental
-- Portability : non-portable
--
-- Day 2. See "AOC.Solver" for the types used in this module!
module AOC.Challenge.Day02 (
day02a
, day02b
) where
import AOC.Common.Point (Point)
import AOC.Solver ((:~>)(..))
import Data.Semigroup (Sum(..))
import Linear.V2 (V2(..))
import Text.Read (readMaybe)
import qualified Data.Monoid.Action as Mo
import qualified Data.Monoid.SemiDirectProduct.Strict as Mo
day02a :: [Sum Point] :~> Int
day02a = day02
(\x -> Sum $ V2 x 0)
(\y -> Sum $ V2 0 y)
getSum
day02b :: [Mo.Semi (Sum Point) Aim] :~> Int
day02b = day02
(\x -> Mo.inject (Sum (V2 x 0)))
(\a -> Mo.embed (Aim a))
(getSum . Mo.untag)
-- | The difference between Part 1 and Part 2 is just a different monoid
day02
:: Monoid r
=> (Int -> r) -- ^ forward
-> (Int -> r) -- ^ up/down
-> (r -> Point) -- ^ re-extract position
-> [r] :~> Int
day02 f g ext = MkSol
{ sParse = traverse parseAsDir . lines
, sShow = show
, sSolve = Just . product @V2 . ext . mconcat
}
where
parseAsDir ln = do
dir:n:_ <- Just $ words ln
amnt <- readMaybe n
case dir of
"forward" -> Just $ f amnt
"down" -> Just $ g amnt
"up" -> Just $ g (-amnt)
_ -> Nothing
newtype Aim = Aim Int
deriving via Sum Int instance Semigroup Aim
deriving via Sum Int instance Monoid Aim
instance Mo.Action Aim (Sum Point) where
act (Aim a) (Sum (V2 x y)) = Sum (V2 x (y + a * x))