-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay03.hs
161 lines (126 loc) · 4.27 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
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
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE TupleSections #-}
module Day03 where
import Control.Arrow ((&&&))
import Data.Foldable
import Data.Function (on)
import Data.List.NonEmpty (NonEmpty)
import Data.Maybe
import Text.ParserCombinators.ReadP
import Text.Read (readMaybe)
import Harness
import ParseHelper
import Data.Array qualified as A
import Data.List qualified as L
import Data.List.NonEmpty qualified as NEL
main :: IO ()
main = getInputAndSolve (parseInputRaw parsePartNumbers) calcPartNumberSum calcGearRatioSum
-- SOLVE
calcPartNumberSum :: [PartNumber] -> Int
calcPartNumberSum =
sum . map (.fullNumber) . filter (.symbolAdjacent)
calcGearRatioSum :: [PartNumber] -> Int
calcGearRatioSum =
sum
. map calcRatio
. filter isPair
. L.groupBy ((==) `on` fst)
. L.sortOn fst
. concatMap (\pn -> (,pn) <$> pn.adjacentStars)
where
isPair :: [a] -> Bool
isPair = \case
[_, _] -> True
_ -> False
calcRatio :: [(a, PartNumber)] -> Int
calcRatio = product . map ((.fullNumber) . snd)
-- HELPERS
extractPartNumbers :: PointGrid -> [PartNumber]
extractPartNumbers pg@(PointGrid grid) =
let ((xMin, yMin), (xMax, yMax)) = A.bounds grid
in concat
[ findPartNumbersInRow pg row $
catMaybes
[ case grid A.! (col, row) of
Digit i -> Just (i, col)
_ -> Nothing
| col <- [xMin .. xMax]
]
| row <- [yMin .. yMax]
]
findPartNumbersInRow :: PointGrid -> Int -> [(Int, Int)] -> [PartNumber]
findPartNumbersInRow (PointGrid grid) row = buildLastNumber . foldl' go ([], Nothing)
where
go :: ([PartNumber], Maybe (NonEmpty (Int, Int))) -> (Int, Int) -> ([PartNumber], Maybe (NonEmpty (Int, Int)))
go (builtNumbers, inProgress) this@(_, col) = case inProgress of
Nothing ->
(builtNumbers, Just $ return this)
Just nel ->
let prevCol = snd $ NEL.last nel
in if col /= succ prevCol
then (buildNumber nel : builtNumbers, Just $ return this)
else (builtNumbers, Just $ nel <> return this)
buildLastNumber :: ([PartNumber], Maybe (NonEmpty (Int, Int))) -> [PartNumber]
buildLastNumber (built, mbToBuild) = case mbToBuild of
Nothing -> built
Just toBuild -> buildNumber toBuild : built
buildNumber :: NonEmpty (Int, Int) -> PartNumber
buildNumber digits =
let cols = snd <$> digits
neighbors =
map (id &&& (grid A.!))
. L.nub
$ concatMap
(\col -> A.getGridNeighborsDiagonal grid (col, row))
cols
in PartNumber
{ fullNumber = digitListToNumber . NEL.toList $ fmap fst digits
, symbolAdjacent = any (hasSymbol . snd) neighbors
, adjacentStars = map fst $ filter ((== Symbol '*') . snd) neighbors
}
digitListToNumber :: [Int] -> Int
digitListToNumber = foldl' (\acc i -> acc * 10 + i) 0
-- PARSE
data PartNumber = PartNumber
{ fullNumber :: Int
, symbolAdjacent :: Bool
, adjacentStars :: [(Int, Int)]
}
deriving (Eq, Show)
parsePartNumbers :: ReadP [PartNumber]
parsePartNumbers = do
!pg <- parsePointGrid
return $ extractPartNumbers pg
data Point
= Digit Int
| Symbol Char
| Blank
deriving (Eq, Show)
hasSymbol :: Point -> Bool
hasSymbol = \case
Symbol _ -> True
_ -> False
newtype PointGrid
= PointGrid (A.Array (Int, Int) Point)
instance Show PointGrid where
show (PointGrid g) = showPointGrid g
showPointGrid :: A.Array (Int, Int) Point -> String
showPointGrid = A.showGridWith $ \case
Blank -> "."
Symbol c -> [c]
Digit i -> show i
parsePointGrid :: ReadP PointGrid
parsePointGrid = do
PointGrid . fmap parseChar <$> parseCharGrid validChar <* newline
where
validChar :: Char -> Bool
validChar = (/= '\n')
parseChar :: Char -> Point
parseChar c = case readMaybe [c] of
Just i -> Digit i
Nothing -> case c of
'.' -> Blank
_ -> Symbol c