-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.hs
165 lines (147 loc) · 4.91 KB
/
Day13.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
{-# LANGUAGE TemplateHaskell #-}
module Day13
( part1
, part2
) where
import Data.Array.Unboxed (UArray, array, bounds, (!))
import Data.ByteString (ByteString)
import Data.Maybe (Maybe (Just, Nothing), fromJust,
isJust)
import FlatParse.Stateful (ask, char, eof, get, local,
optional_, put, runParser, satisfy,
some)
import qualified Helpers.Parsers.ByteString as B (lines)
import Helpers.Parsers.FlatParse (ParserS, extractS)
import Linear.V2 (V2 (..))
type Pos = V2 Int
type Pat = UArray Pos Char
data Direction
= V
| H
deriving (Show, Eq, Ord)
parseInput :: ParserS Int [Pat]
parseInput = some parsePat <* eof
parsePat :: ParserS Int Pat
parsePat =
put 0
>> (concat <$> some parseLine)
>>= \l ->
optional_ $(char '\n')
>> pure (array (V2 0 0, maximum . map fst $ l) l)
parseLine :: ParserS Int [(Pos, Char)]
parseLine =
get >>= \y ->
put 0
>> (local (const y) . some $ parseChar)
>>= \l -> put (y + 1) >> $(char '\n') >> pure l
parseChar :: ParserS Int (Pos, Char)
parseChar =
ask >>= \y ->
get >>= \x ->
satisfy (`elem` ".#") >>= \c -> put (x + 1) >> pure (V2 x y, c)
findAxis :: Pat -> (Int, Direction)
findAxis pat
| isJust va = (fromJust va, V)
| isJust ha = (fromJust ha, H)
| otherwise = error ("axis not found for pattern\n" ++ show pat)
where
va = findVerticalAxis 0 pat
ha = findHorizontalAxis 0 pat
-- Our arrays start at 0 where in the problem they start at 1, so we need to add
-- 1 to the result we find
findVerticalAxis :: Int -> Pat -> Maybe Int
findVerticalAxis axis pat
| axis >= width = Nothing
| symmetric = Just (axis + 1)
| otherwise = findVerticalAxis (axis + 1) pat
where
(_, V2 width height) = bounds pat
term = min (axis + 1) (width - axis)
backward =
map
(\x -> map (\y -> pat ! V2 x y) [0 .. height])
[axis,(axis - 1) .. (axis - term)]
forward =
map
(\x -> map (\y -> pat ! V2 x y) [0 .. height])
[(axis + 1) .. (axis + term)]
symmetric = and . zipWith (==) backward $ forward
findHorizontalAxis :: Int -> Pat -> Maybe Int
findHorizontalAxis axis pat
| axis >= height = Nothing
| symmetric = Just (axis + 1)
| otherwise = findHorizontalAxis (axis + 1) pat
where
(_, V2 width height) = bounds pat
term = min (axis + 1) (height - axis)
backward =
map
(\y -> map (\x -> pat ! V2 x y) [0 .. width])
[axis,(axis - 1) .. (axis - term)]
forward =
map
(\y -> map (\x -> pat ! V2 x y) [0 .. width])
[(axis + 1) .. (axis + term)]
symmetric = and . zipWith (==) backward $ forward
findSmudgedAxis :: Pat -> (Int, Direction)
findSmudgedAxis pat
| isJust va = (fromJust va, V)
| isJust ha = (fromJust ha, H)
where
va = findSmudgedVerticalAxis 0 pat
ha = findSmudgedHorizontalAxis 0 pat
findSmudgedVerticalAxis :: Int -> Pat -> Maybe Int
findSmudgedVerticalAxis axis pat
| axis >= width = Nothing
| quasiSymmetric = Just (axis + 1)
| otherwise = findSmudgedVerticalAxis (axis + 1) pat
where
(_, V2 width height) = bounds pat
term = min (axis + 1) (width - axis)
backward =
map
(\x -> map (\y -> pat ! V2 x y) [0 .. height])
[axis,(axis - 1) .. (axis - term)]
forward =
map
(\x -> map (\y -> pat ! V2 x y) [0 .. height])
[(axis + 1) .. (axis + term)]
diffPairs = filter (uncurry (/=)) . zip backward $ forward
justTheOne = length diffPairs == 1
(bef, aft) = head diffPairs
smudged = (length . filter (uncurry (/=)) . zip bef $ aft) == 1
quasiSymmetric = justTheOne && smudged
findSmudgedHorizontalAxis :: Int -> Pat -> Maybe Int
findSmudgedHorizontalAxis axis pat
| axis >= height = Nothing
| quasiSymmetric = Just (axis + 1)
| otherwise = findSmudgedHorizontalAxis (axis + 1) pat
where
(_, V2 width height) = bounds pat
term = min (axis + 1) (height - axis)
backward =
map
(\y -> map (\x -> pat ! V2 x y) [0 .. width])
[axis,(axis - 1) .. (axis - term)]
forward =
map
(\y -> map (\x -> pat ! V2 x y) [0 .. width])
[(axis + 1) .. (axis + term)]
diffPairs = filter (uncurry (/=)) . zip backward $ forward
justTheOne = length diffPairs == 1
(bef, aft) = head diffPairs
smudged = (length . filter (uncurry (/=)) . zip bef $ aft) == 1
quasiSymmetric = justTheOne && smudged
score :: (Int, Direction) -> Int
score (i, V) = i
score (i, _) = i * 100
part1 :: Bool -> ByteString -> String
part1 _ =
show . sum . map (score . findAxis) . extractS . runParser parseInput 0 0
part2 :: Bool -> ByteString -> String
part2 _ =
show
. sum
. map (score . findSmudgedAxis)
. extractS
. runParser parseInput 0 0