-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
50 lines (40 loc) · 1.19 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
{-# LANGUAGE TupleSections #-}
import AoC.Search (bfs_)
import Control.Monad (guard)
import Data.Bits
import Data.Maybe (fromJust)
f x y = x*x + 3*x + 2*x*y + y + y*y
wall :: (Bits a, Num a) => a -> a -> a -> Bool
wall input x y = odd (popCount (f x y + input))
drawCell input x y =
if wall input x y
then '#'
else '.'
printGrid :: Int -> Int -> Int -> String
printGrid input w h =
unlines [[drawCell input x y | x <- [0..w]] | y <- [0..h]]
woop :: [(a -> a)] -> (a, a) -> [(a, a)]
woop fs (x, y) = do
f <- fs
[(f x, y), (x, f y)]
neighboursOf :: Integer -> (Integer, Integer) -> [(Integer, Integer)]
neighboursOf input pos = do
(x', y') <- woop [(+1), (+ negate 1)] pos
guard $ x' >= 0 && y' >= 0
guard $ not $ wall input x' y'
pure (x', y')
minCost input target = bfs_ (== target) (neighboursOf input) (1, 1)
part1 input =
fromJust $ minCost input (31, 39)
-- TODO: Limit bfs search to stop after 50 steps.
part2 input = length $ do
target <- [(x, y) | x <- [0..50], y <- [0..50], x + y <= 50]
case minCost input target of
Just steps -> do
guard $ steps <= 50
pure target
Nothing -> []
main = do
let input = 1358
print (part1 input)
print (part2 input)