-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay6.hs
32 lines (27 loc) · 929 Bytes
/
Day6.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
module Day6
( part1
, part2
) where
import Data.ByteString (ByteString)
import qualified Data.ByteString as B (filter)
import Data.Word8 (_space)
import Helpers.Parsers.ByteString (signedDoubles)
toPair :: [[Double]] -> [(Double, Double)]
toPair (a:b:_) = zip a b
-- The problem boils down to x(t - x) > r, that is x² - tx + r < 0,
-- that is any integer between the roots of x^2 - tx + r
quadraticSolution :: (Double, Double) -> Int
quadraticSolution (t, r) = maxX - minX + 1
where
minX = ceiling ((t - sqrt (t ^ 2 - 4 * r)) / 2)
maxX = floor ((t + sqrt (t ^ 2 - 4 * r)) / 2)
part1 :: Bool -> ByteString -> String
part1 _ = show . product . map quadraticSolution . toPair . signedDoubles
part2 :: Bool -> ByteString -> String
part2 _ =
show
. quadraticSolution
. head
. toPair
. signedDoubles
. B.filter (/= _space)