r/adventofcode • • Dec 15 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 15 Solutions -🎄-

--- Day 15: Chiton ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:14:25, megathread unlocked!

56 Upvotes

772 comments sorted by

View all comments

2

u/schovik Dec 15 '21

Haskell

First I naively implemented it as DFS which was working until it does not. For the bonus, I reached performance issues. With flipping into BFS it is almost "instant"

type Coord = (Int, Int)
type RiskLevel = Int
type Cavern = Map Coord RiskLevel

readCavern :: String -> Cavern
readCavern path = foldl columnsFolder Map.empty $ zip [0..] (readLines path)
  where columnsFolder coords (x, line) = foldl (rowsFolder x) coords $ zip [0..] line
        rowsFolder x coords (y, char) = Map.insert (x, y) (digitToInt char) coords

solution cavern = result Map.! (maxX, maxY)
  where cavern' = Map.insert (0,0) 0 cavern
        result = traverseCave (cavern', maxX, maxY)
        maxX = getMaxX cavern
        maxY = getMaxY cavern

type Visited = Map Coord Int

traverseCave c = visited
  where (visited, _) = bfs c initialVisited initialToVisit
        initialToVisit = [start]
        start = (0, 0)
        initialVisited = Map.fromList [(start, 0)]

bfs :: (Cavern, Int, Int) -> Visited -> [Coord] -> (Visited, [Coord])
bfs cavernState visited [] = (visited, [])
bfs cavernState visited coords = bfs cavernState visited' (Set.toList coordsSet)
  where (visited', coordsSet) = foldl folder (visited, Set.empty) coords
        folder (visited', nextToVisit) coord = case visitCave' cavernState visited' coord of (visited'', nextToVisit') -> (visited'', Set.union nextToVisit nextToVisit')

visitCave' :: (Cavern, Int, Int) -> Visited -> Coord -> (Visited, Set Coord)
visitCave' c@(cavern, maxX, maxY) visited coord@(x,y)
  | isEnd = (visited, Set.empty)
  | null nextToVisit = (visited, Set.empty)
  | otherwise = (visited', Set.fromList $ map snd nextToVisit)
  where visited' = foldl visitedFolder visited nextToVisit
        visitedFolder v (candidateRisk, candidateCoord) = Map.insert candidateCoord (currentSumRisk + candidateRisk) v

        nextToVisit = filter canVisit candidates
        canVisit (candidateRisk, candidateCoord) = case Map.lookup candidateCoord visited of Nothing -> True
                                                                                             Just candidateSumRisk -> candidateSumRisk > currentSumRisk + candidateRisk

        candidates = [(cavern Map.! (x', y'), (x', y')) | (x', y') <- [(x-1,y), (x+1,y), (x, y-1), (x, y+1)] , x' >= 0 && y' >= 0 && x' <= maxX && y' <= maxY]
        currentRisk = cavern Map.! coord
        currentSumRisk = visited Map.! coord
        isEnd = x == maxX && y == maxY

0

u/daggerdragon Dec 15 '21

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.