-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day02.hs
76 lines (68 loc) · 1.73 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
66
67
68
69
70
71
72
73
74
75
-- |
-- 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 (Dir(..), Point, parseDir, dirPoint)
import AOC.Solver ((:~>)(..))
import Data.List (mapAccumL, foldl')
import Data.Map (Map)
import Data.Maybe (mapMaybe)
import Linear (V2(..))
import qualified Data.Map as M
stepper :: Map Point Char -> Point -> [Dir] -> (Point, Maybe Char)
stepper mp x = (\r -> (r, M.lookup r mp))
. foldl' move x
where
move p d
| p' `M.member` mp = p'
| otherwise = p
where
p' = p + dirPoint d
keypadA :: Map Point Char
keypadA = M.fromList . flip zip ['1'..] $
[ V2 (-1) 1
, V2 0 1
, V2 1 1
, V2 (-1) 0
, V2 0 0
, V2 1 0
, V2 (-1) (-1)
, V2 0 (-1)
, V2 1 (-1)
]
day02a :: [[Dir]] :~> String
day02a = MkSol
{ sParse = Just . (map . mapMaybe) parseDir . lines
, sShow = id
, sSolve = sequence . snd . mapAccumL (stepper keypadA) 0
}
keypadB :: Map Point Char
keypadB = M.fromList . flip zip (['1'..'9'] ++ ['A'..]) $
[ V2 0 2
, V2 (-1) 1
, V2 0 1
, V2 1 1
, V2 (-2) 0
, V2 (-1) 0
, V2 0 0
, V2 1 0
, V2 2 0
, V2 (-1) (-1)
, V2 0 (-1)
, V2 1 (-1)
, V2 0 (-2)
]
day02b :: [[Dir]] :~> String
day02b = MkSol
{ sParse = Just . (map . mapMaybe) parseDir . lines
, sShow = id
, sSolve = sequence . snd . mapAccumL (stepper keypadB) (V2 (-2) 0)
}