-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.hs
34 lines (24 loc) · 780 Bytes
/
06.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
import Control.Monad
import Data.Bifunctor
import Lib
type Time = Int
type Distance = Int
type Input = ([Time], [Distance])
main :: IO ()
main = aoc 2023 6 setup solve1 solve2 ["1"]
solve1 :: Input -> Int
solve1 = product . map (uncurry solve) . uncurry zip
solve2 :: Input -> Int
solve2 = uncurry solve . join bimap joinInts
solve :: Time -> Distance -> Int
solve time distance =
let x = time * time - distance * 4
root = floor . sqrt . fromIntegral $ x
root_is_int = fromEnum (root * root == x)
in root + (time + root + 1 + root_is_int) `mod` 2 - root_is_int
setup :: String -> Input
setup = listToTuple . map parseLine . lines
parseLine :: String -> [Int]
parseLine = map read . tail . words
joinInts :: [Int] -> Int
joinInts = read . concatMap show