Skip to content

Commit e46ad8d

Browse files
committed
refaco(2024/Day16)
1 parent 1e10249 commit e46ad8d

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

2024/Day16/Day16.hs

+13-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
module Main where
22

3-
import Data.Bits
43
import Data.List
5-
import Data.List.Split
6-
import Data.List.Unique
74
import Data.Map (Map)
85
import Data.Map qualified as M
96
import Data.Matrix (Matrix, (!))
@@ -13,7 +10,6 @@ import Data.Set qualified as S
1310
import Data.Tuple.Extra
1411
import Debug.Trace
1512
import System.Environment
16-
import Text.Regex.TDFA ((=~))
1713
-- TODO: Cleanup imports after day done
1814

1915
type Input = (Matrix Char, (Int, Int))
@@ -42,41 +38,31 @@ turn90 RIGHT = [UP, DOWN]
4238

4339
pathFind ::Matrix Char -> Map ((Int, Int), Dir) Int -> Int -> Dir -> (Int, Int) -> (Bool, Map ((Int, Int), Dir) Int, Int, Set (Int, Int))
4440
pathFind maze seen score dir pos
45-
-- | rightPos && trace ("At " ++ show pos) (False ) = (False, seen, -1, S.empty)
46-
| (pos, dir) `M.member` seen && seen M.! (pos, dir) < score = (False, seen, -1, S.empty) -- Lower score ok
47-
-- | rightPos && trace ("At " ++ show pos ++ " has better score") (False ) = (False, seen, -1, S.empty)
48-
| maze ! pos == 'E' = (True, seen, score, S.singleton pos)
49-
-- | rightPos && trace ("At " ++ show pos ++ " not at end") (False ) = (False, seen, -1, S.empty)
50-
| maze ! pos == '#' = (False, seen, -1, S.empty)
51-
-- | rightPos && trace ("At " ++ show pos ++ " not at wall" ++ show bestSols) (False ) = (False, seen, -1, S.empty)
41+
| {-# SCC alreadyBeen #-} (pos, dir) `M.member` seen && seen M.! (pos, dir) < score = (False, seen, -1, S.empty) -- Lower score ok
42+
| {-# SCC isEnd #-} maze ! pos == 'E' = (True, seen, score, S.singleton pos)
43+
| {-# SCC isWall #-} maze ! pos == '#' = (False, seen, -1, S.empty)
5244
| length bestSols /= 0 = bestSol
5345
| otherwise = (False, s2seen, -1, S.empty)
5446
where
55-
rightPos = (pos == (5, 4) || (pos == (4, 4) && dir == UP))
56-
57-
58-
5947
currSeen = (M.insert (pos, dir) score seen)
60-
(fbool, fseen, fscore, fseats) = pathFind maze currSeen (score + 1) dir (move pos dir)
48+
(fbool, fseen, fscore, fseats) = {-# SCC findForward #-} pathFind maze currSeen (score + 1) dir (move pos dir)
6149
[d1, d2] = turn90 dir
62-
(s1bool, s1seen, s1score, s1seats) = pathFind maze fseen (score + 1001) d1 (move pos d1)
63-
(s2bool, s2seen, s2score, s2seats) = pathFind maze s1seen (score + 1001) d2 (move pos d2)
50+
(s1bool, s1seen, s1score, s1seats) = {-# SCC findSide1 #-} pathFind maze fseen (score + 1001) d1 (move pos d1)
51+
(s2bool, s2seen, s2score, s2seats) = {-# SCC findSide2 #-} pathFind maze s1seen (score + 1001) d2 (move pos d2)
6452

6553
bestSols :: [[(Bool, Set (Int, Int), Int)]]
66-
bestSols = groupBy (\a b -> thd3 a == thd3 b) . sortOn thd3 $ filter (fst3) [(fbool, fseats, fscore), (s1bool, s1seats, s1score), (s2bool, s2seats, s2score)]
54+
bestSols = {-# SCC sortBest #-} groupBy (\a b -> thd3 a == thd3 b) . sortOn thd3 $ filter (fst3) [(fbool, fseats, fscore), (s1bool, s1seats, s1score), (s2bool, s2seats, s2score)]
6755

68-
seats = S.unions $ map snd3 $ head bestSols
56+
seats = {-# SCC union #-} S.unions $ map snd3 $ head bestSols
6957

7058
bestSol = (True, s2seen, thd3 . head $ head bestSols, S.insert pos seats)
7159

72-
part :: Input -> (Output, Output)
73-
part (maze, pos) = (\(_,_,p1,p2) -> (p1, S.size p2)) $ pathFind maze M.empty 0 RIGHT pos
60+
solve :: Input -> (Output, Output)
61+
solve (maze, pos) = (\(_,_,p1,p2) -> (p1, {-# SCC sizeCalculation #-} S.size p2)) $ pathFind maze M.empty 0 RIGHT pos
7462

7563
showMaze :: Matrix Char -> Set (Int, Int) -> [String]
7664
showMaze maze path = map (map (\yx -> if yx `S.member` path then 'O' else maze ! yx)) [[(y, x) | x<-[1..Mat.ncols maze]] | y<-[1..Mat.nrows maze]]
7765

78-
79-
8066
main :: IO ()
8167
main = do
8268
args <- getArgs
@@ -85,9 +71,9 @@ main = do
8571

8672
-- print $ first Mat.toLists input
8773

88-
let (part1, part2) = part input
74+
let (part1, part2) = solve input
8975

90-
print $ (== 85480) $ part1
91-
print $ (== 518) $ part2
76+
print $ part1
77+
print $ part2
9278

9379
-- putStrLn $ unlines . showMaze (fst input) $ (\(_,_,_,e) -> e) $ pathFind (fst input) M.empty 0 RIGHT (snd input)

2024/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,37 @@ Nice and interesting puzzle today
204204
Basically a simple path finding but with a few twists that make it fun
205205

206206
Will try to optimize solution
207+
208+
Edit: Haven't found any easy optimization
209+
210+
I know a way to optimize : Have another Map containing this time the set of valid spots => if already been on a position no need to do it again even from another good path.
211+
212+
For example
213+
```
214+
#######
215+
#....E#
216+
#.#.###
217+
#S..###
218+
#######
219+
```
220+
221+
The first path explored will be this one
222+
```
223+
#######
224+
#..OOE#
225+
#.#O###
226+
#SOO###
227+
#######
228+
```
229+
230+
But then at the moment when I reach the last intersection I need to go back up to the end
231+
232+
```
233+
#######
234+
#OOXXE#
235+
#O#.###
236+
#S..###
237+
#######
238+
```
239+
240+
The optimization would allow me to skip this last part represented with `X`

0 commit comments

Comments
 (0)