Skip to content

Commit f689615

Browse files
committed
Solve day 17
1 parent 9808a9b commit f689615

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

2021/17/main.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
step (vx, vy) (x, y) = ((vx - signum vx, vy - 1), (x + vx, y + vy))
2+
3+
inArea ((lx, ly), (ux, uy)) (x, y) = f lx ux x && f ly uy y
4+
where f a b v = a <= v && v <= b
5+
6+
simulate t@((_, ly), (ux, _)) vel = (inArea t p, (v, p))
7+
where cond (_, p@(x, y)) = inArea t p || x > ux || y < ly
8+
(v, p) = until cond (uncurry step) (vel, (0, 0))
9+
10+
testVels = [(x, y) | x <- [1..], y <- [x^2+1, x^2..(-(x^2)-1)]]
11+
12+
solve a@(_, (ux, _)) = map (snd . fst)
13+
$ filter (fst . snd)
14+
$ takeWhile c
15+
$ map (\p -> (p, simulate a p)) testVels
16+
where c (_, (_, (_, (x, _)))) = x <= ux * 2
17+
18+
maxHeight y = (y + 1) * y `div` 2
19+
20+
test = ((20, -10), (30, -5))
21+
input = ((128, -142), (160, -88))
22+
23+
main = mapM_ print $ sequence [maxHeight . maximum, length] $ solve input

0 commit comments

Comments
 (0)