-
Notifications
You must be signed in to change notification settings - Fork 8
/
Day03.hs
58 lines (51 loc) · 1.32 KB
/
Day03.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
-- |
-- Module : AOC.Challenge.Day03
-- License : BSD3
--
-- Stability : experimental
-- Portability : non-portable
module AOC.Challenge.Day03 (
day03a
, day03b
, validCoord
) where
import AOC.Common (countTrue)
import AOC.Solver ((:~>)(..))
import Data.Char (isSpace)
import Data.Finite (Finite, modulo)
type Coord = (Finite 31, Int)
validCoord
:: Finite 31 -- ^ dx
-> Int -- ^ dy
-> Coord
-> Bool
validCoord dx dy = \(x,y) ->
let (i,r) = y `divMod` dy
in r == 0 && dx * modulo (fromIntegral i) == x
countLine :: Finite 31 -> Int -> String -> Int
countLine dx dy = countTrue (uncurry tree)
. zip (splitOut <$> [0..])
where
checkCoord = validCoord dx dy
tree xy c = c == '#' && checkCoord xy
splitOut i = (fromIntegral x, y)
where
(!y, !x) = i `divMod` 31
day03a :: String :~> Int
day03a = MkSol
{ sParse = Just . filter (not . isSpace)
, sShow = show
, sSolve = Just . countLine 3 1
}
day03b :: String :~> Int
day03b = MkSol
{ sParse = Just . filter (not . isSpace)
, sShow = show
, sSolve = \s -> Just . product $
[ countLine 1 1
, countLine 3 1
, countLine 5 1
, countLine 7 1
, countLine 1 2
] <*> [s]
}