This repository has been archived by the owner on Nov 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.hs
62 lines (51 loc) · 1.38 KB
/
Day01.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
-- |
-- Module : AOC.Challenge.Day01
-- License : BSD3
--
-- Stability : experimental
-- Portability : non-portable
--
-- Day 1. See "AOC.Solver" for the types used in this module!
module AOC.Challenge.Day01 (
day01a
, day01b
) where
import AOC.Common (Dir(..), Point, parseDir, firstRepeated, mannDist, dirPoint)
import AOC.Solver ((:~>)(..))
import Data.List (foldl')
import Text.Read (readMaybe)
data Turtle = (:@) { tLoc :: Point
, tDir :: Dir
}
deriving Show
data Command = CTurn Dir
| CGo
deriving Show
stepper :: Turtle -> Command -> Turtle
stepper (x :@ h) = \case
CTurn d -> (x + dirPoint h') :@ h'
where
h' = d <> h
CGo -> (x + dirPoint h ) :@ h
day01a :: [Command] :~> Int
day01a = MkSol
{ sParse = parseCmd
, sShow = show
, sSolve = Just . mannDist 0 . tLoc
. foldl' stepper (0 :@ North)
}
day01b :: [Command] :~> Int
day01b = MkSol
{ sParse = parseCmd
, sShow = show
, sSolve = fmap (mannDist 0)
. firstRepeated
. map tLoc
. scanl stepper (0 :@ North)
}
parseCmd :: String -> Maybe [Command]
parseCmd str = Just $ do
x:xs <- words . filter (/=',') $ str
Just h <- pure $ parseDir x
Just n <- pure $ readMaybe xs
CTurn h : replicate (n - 1) CGo