-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day13.hs
136 lines (122 loc) · 3.93 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
module Day13
( part1
, part2
) where
import Helpers.Parsers (make2DArray)
import Data.Array.Unboxed (UArray, bounds, (!))
import Data.List.Split (splitWhen)
import Data.Maybe (Maybe (Just, Nothing), fromJust, isJust)
import Linear.V2 (V2 (..))
type Pos = V2 Int
type Pat = UArray Pos Char
data Direction
= V
| H
deriving (Show, Eq, Ord)
findAxis :: Pat -> (Int, Direction)
findAxis pat
| isJust va = (fromJust va, V)
| isJust ha = (fromJust ha, H)
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 -> String -> String
part1 _ =
show .
sum .
map (score . findAxis . make2DArray) .
splitWhen null . map (filter (`elem` "#.")) . lines
part2 :: Bool -> String -> String
part2 _ =
show .
sum .
map (score . findSmudgedAxis . make2DArray) .
splitWhen null . map (filter (`elem` "#.")) . lines