Skip to content

Commit 0b512bb

Browse files
day 18
1 parent 1cebf10 commit 0b512bb

File tree

5 files changed

+3507
-2
lines changed

5 files changed

+3507
-2
lines changed

Diff for: Algorithms.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Algorithms (shortestPath, Dist(Dist)) where
1+
module Algorithms (shortestPath, Dist(Dist, Inf), show) where
22

33
import qualified Data.Map.Strict as M
44
import qualified Data.Set as S

Diff for: BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ haskell_binary(
3838
ghcopts = EXTENSIONS,
3939
)
4040

41-
DAYS = 17
41+
DAYS = 18
4242

4343
[haskell_binary(
4444
name = "d{}".format(day + 1),

Diff for: Day18.hs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import qualified Data.Attoparsec.Text as At
2+
import qualified Data.Map.Strict as M
3+
import qualified Data.Set as S
4+
import Control.Monad
5+
import Control.Applicative
6+
import Data.Maybe (isNothing, catMaybes)
7+
import Control.Parallel.Strategies
8+
import Debug.Trace
9+
10+
import Commons
11+
import Algorithms
12+
13+
main :: IO ()
14+
main = do
15+
blocks <- inp (At.sepBy1 (V2 <$> (At.decimal <* At.char ',') <*> At.decimal) At.endOfLine)
16+
let n = 70
17+
emptyGrid = M.fromList [(V2 i j, '.') | i <- [0..n], j <- [0..n]]
18+
fallen = 1024
19+
dirs = [V2 0 1, V2 1 0, V2 0 (-1), V2 (-1) 0]
20+
neighbor g v = [(v + d, Dist 1) | d <- dirs, M.member (v + d) g, g M.! (v + d) == '.']
21+
grid used = M.union (M.fromList $ map (,'#') (take used blocks)) emptyGrid :: M.Map V2i Char
22+
cost g = shortestPath (neighbor g) 0
23+
end = V2 n n
24+
print $ cost (grid fallen) M.! end
25+
26+
let ends i = if isNothing endDist || endDist == Just Inf then Just (i - 1) else Nothing
27+
where run = cost (grid i) `using` rpar
28+
endDist = M.lookup end run
29+
costs = parMap rpar ends [fallen..length blocks]
30+
print $ (blocks !!) $ head $ catMaybes costs

0 commit comments

Comments
 (0)