-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.hs
71 lines (55 loc) · 1.48 KB
/
run.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
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
import AoC
import AoC.Grid
import Data.Bifunctor
import Data.Foldable
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Ord
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
type N = Integer
type F = Double
t2 [x, y] = (x, y)
parse = map (read @F) . drop 1 . words
parseAll = t2 . map parse . lines
parseJoined = read @F . concat . drop 1 . words
parseOne = t2 . map parseJoined . lines
-- x*(t-x) > d
-- -x² + xt - d > 0
-- x² - xt + d < 0 (
-- x = ½t +- sqrt(t²/4 - d)
-- ½t - sqrt(t²/4 - d) < x < ½t + sqrt(t²/4 - d)
ineq t d =
let root = sqrt (t^2 / 4 - d)
in
(t/2 - root, t/2 + root)
rounded (l, u) = (ceiling l, floor u)
counts (l, u) = u - l + 1
solve =
counts . rounded . uncurry ineq
part1 (times, distances) =
product . map solve $ zip times distances
part2 = solve
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- readFile file
print (part1 $ parseAll input)
print (part2 $ parseOne input)