-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_17.hs
39 lines (31 loc) · 1.21 KB
/
day_17.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
module Main where
type Position = (Int, Int)
type Velocity = (Int, Int)
type State = (Position, Velocity)
type Target = (Position, Position)
step :: State -> State
step ((x, y), (vx, vy))
| vx == 0 = ((x+vx, y+vy), (vx , vy - 1))
| vx < 0 = ((x+vx, y+vy), (vx + 1, vy - 1))
| vx > 0 = ((x+vx, y+vy), (vx - 1, vy - 1))
isWithingTarget :: State -> Target -> Bool
isWithingTarget ((x,y), _) ((xmin, xmax), (ymin, ymax)) = x >= xmin && x <= xmax && y >= ymin && y <= ymax
isStillValid :: State -> Target -> Bool
isStillValid ((x,y), _) (_, (ymin, ymax)) = y >= ymin
isValid :: State -> Target -> Bool
isValid s t = do
if not $ isStillValid s t then False else if isWithingTarget s t then True else isValid (step s) t
findMaxY :: State -> Target -> Int
findMaxY ((x, y), v) t = do
let s2 = step ((x, y), v)
if (snd $ fst s2) < y then y else findMaxY s2 t
bruteForce :: Target -> [State]
bruteForce t = do
let brute = [((0,0), (x, y)) | x <- [-300..300], y <- [-300..300]]
filter (\e -> isValid e t) brute
main :: IO()
main = do
let target = ((241, 275), (-75, -49))
let targets = bruteForce target
print $ maximum $ map (\e -> findMaxY e target) targets
print $ length $ targets