aboutsummaryrefslogtreecommitdiff
path: root/2023/16-The_Floor_Will_Be_Lava/second.hs
blob: cff774434ed4b147e93a8d1c8e4cb42025cd5242 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
-- requires cabal install --lib megaparsec parser-combinators
module Main (main) where

import Control.Applicative.Permutations
import Control.Monad (void, when)
import Data.Char qualified as C
import Data.Either
import Data.Functor
import Data.List qualified as L
import Data.Map qualified as M
import Data.Maybe
import Data.Set qualified as S
import Data.Vector qualified as V
import qualified Data.Vector.Unboxed as VU
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char

import Debug.Trace

exampleExpectedOutput = 51

data Tile = Backslash | Empty | Horizontal | Slash | Vertical deriving Eq
instance Show Tile where
  show Backslash = "\\"
  show Empty = "."
  show Horizontal = "-"
  show Slash = "/"
  show Vertical = "|"
type Line = V.Vector Tile
type Input = V.Vector Line

type Parser = Parsec Void String

parseTile :: Parser Tile
parseTile = char '\\' $> Backslash
        <|> char '.' $> Empty
        <|> char '-' $> Horizontal
        <|> char '/' $> Slash
        <|> char '|' $> Vertical

parseLine :: Parser Line
parseLine = do
  line <- some parseTile <* eol
  return $ V.generate (length line) (line !!)

parseInput' :: Parser Input
parseInput' = do
  line <- some parseLine <* eof
  return $ V.generate (length line) (line !!)

parseInput :: String -> IO Input
parseInput filename = do
  input <- readFile filename
  case runParser parseInput' filename input of
    Left bundle -> error $ errorBundlePretty bundle
    Right input' -> return input'

type LineOutput = VU.Vector Bool
type Output = V.Vector LineOutput
data Heading = N | S | E | W deriving (Eq, Ord, Show)
data Cursor = Cursor Int Int Heading deriving (Eq, Ord, Show)

type Visited = M.Map Cursor ()

next :: Cursor -> Cursor
next (Cursor x y N) = Cursor x (y-1) N
next (Cursor x y S) = Cursor x (y+1) S
next (Cursor x y W) = Cursor (x-1) y W
next (Cursor x y E) = Cursor (x+1) y E

compute' :: Input -> Cursor -> Int
compute' input startingCursor = V.foldl' count 0 . fst $ walk startingOutput M.empty startingCursor
  where
    count :: Int -> LineOutput -> Int
    count i l = i + VU.length (VU.filter id l)
    startingOutput = V.replicate size $ VU.replicate size False
    size = V.length input
    walk :: Output -> Visited -> Cursor -> (Output, Visited)
    walk output v c | isNothing nextTile = (output, v)
                    | justNextTile == Empty = walk output' v c'
                    | visited = (output, v)
                    | justNextTile == Horizontal = if h == W || h == E then walk output' v c'
                                                                       else let (output'', v'') = walk output' v' (Cursor x y E) in walk output'' v'' $ Cursor x y W
                    | justNextTile == Vertical = if h == N || h == S then walk output' v c'
                                                                     else let (output'', v'') = walk output' v' (Cursor x y N) in walk output'' v'' $ Cursor x y S
                    | justNextTile == Backslash = case h of
                        N -> walk output' v' $ Cursor x y W
                        S -> walk output' v' $ Cursor x y E
                        W -> walk output' v' $ Cursor x y N
                        E -> walk output' v' $ Cursor x y S
                    | justNextTile == Slash = case h of
                        N -> walk output' v' $ Cursor x y E
                        S -> walk output' v' $ Cursor x y W
                        W -> walk output' v' $ Cursor x y S
                        E -> walk output' v' $ Cursor x y N
      where
        c'@(Cursor x y h) = next c
        nextTile = case input V.!? y of
          Just l -> l V.!? x
          Nothing -> Nothing
        justNextTile = fromJust nextTile
        line = output V.! y
        visited = M.member c' v
        v' = M.insert c' () v
        output' = if visited then output
                             else output V.// [(y, line VU.// [(x, True)])]

compute :: Input -> Int
compute input = maximum $ map (compute' input) cursors
  where
    cursors = [Cursor (-1) y E | y <- [0..l]]
           ++ [Cursor l y W | y <- [0..l]]
           ++ [Cursor x (-1) S | x <- [0..l]]
           ++ [Cursor x l N | x <- [0..l]]
    l = V.length input

main :: IO ()
main = do
  example <- parseInput "example"
  let exampleOutput = compute example
  when  (exampleOutput /= exampleExpectedOutput)  (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
  input <- parseInput "input"
  print $ compute input