8000 polish Day16 · opqdonut/adventofcode24@6faed15 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6faed15

Browse files
committed
polish Day16
1 parent a7cf12c commit 6faed15

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Day16.hs

+8-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ turns R = [U,D]
3333
(x,y) >>> L = (x-1,y)
3434
(x,y) >>> R = (x+1,y)
3535

36-
nexts (cost,pos,dir,ps) = (cost+1,pos >>> dir, dir, pos:ps):[(cost+1000,pos,dir',ps) | dir' <- turns dir]
36+
type State = (Int,Pos,Dir,[Pos]) -- (cost,pos,dir,previous positions)
37+
38+
neighbours :: State -> [State]
39+
neighbours (cost,pos,dir,ps) = (cost+1,pos >>> dir, dir, pos:ps):[(cost+1000,pos,dir',ps) | dir' <- turns dir]
3740

3841
orderedUnion new old = foldr insert old (sort new)
3942

@@ -49,12 +52,13 @@ visualize paths visited = unlines [ [ r (x,y) | x <- [0..maxX] ] | y <- [0..maxY
4952

5053
dijkstra :: (Pos,Pos,Paths) -> [(Int,[Pos])]
5154
dijkstra (start,end,paths) = go S.empty [(0,start,R,[])]
52-
where go :: S.Set (Pos,Dir) -> [(Int,Pos,Dir,[Pos])] -> [(Int,[Pos])]
55+
where go :: S.Set (Pos,Dir) -> [State] -> [(Int,[Pos])]
5356
go visited (s@(cost,pos,dir,_):ss)
54-
| pos == end = [(cost,pos:ps) | (cost,pos,_,ps) <- s:ss, pos==end]
57+
| pos == end = [(cost,pos:ps) | (cost',pos',_,ps) <- s:ss, pos'==end, cost'==cost]
5558
| otherwise = --trace (visualize paths visited) $
5659
--trace ("Q: "++show (s:ss)) $
57-
go (S.insert (pos,dir) visited) (orderedUnion (filter (possible visited) $ nexts s) ss)
60+
go (S.insert (pos,dir) visited) (orderedUnion (nexts visited s) ss)
61+
nexts visited s = filter (possible visited) $ neighbours s
5862
possible visited (_,p,dir,_) = S.member p paths && not (S.member (p,dir) visited)
5963

6064
part1 = fst . head . dijkstra

0 commit comments

Comments
 (0)
0