summaryrefslogtreecommitdiff
path: root/2017/19.hs
blob: d6b2d47dfd21a99051744e5ae0b3aaa7e9f4b638 (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
import Prelude hiding (Right, Left, flip)
import Control.Monad
import Data.Array.Unboxed
import Data.Char


type Idx = (Int, Int)
type Chart = UArray Idx Char
data Dir = Up | Right | Down | Left
  deriving (Show, Eq)

add :: Idx -> Dir -> Idx
add (y, x) dir = case dir of
    Up -> (y - 1, x)
    Right -> (y, x + 1)
    Down -> (y + 1, x)
    Left -> (y, x - 1)

flip :: Dir -> Dir
flip Down = Up
flip Up = Down
flip Right = Left
flip Left = Right

corresponds :: Char -> Dir -> Bool
corresponds '|' dir = dir `elem` [Down, Up]
corresponds '-' dir = dir `elem` [Right, Left]
corresponds '+' _ = True
corresponds c _ | isAlpha c = True
corresponds ' ' _ = False
corresponds c d = error ("corresponds: " ++ show c ++ " " ++ show d)

steer :: Chart -> Idx -> Char -> Dir -> Dir
steer _ _ '|' Down = Down
steer _ _ '|' Up = Up
steer _ _ '|' dir = dir
steer _ _ '-' Right = Right
steer _ _ '-' Left = Left
steer _ _ '-' dir = dir
steer chart idx '+' dir =
    let [newdir] = [dir' | dir' <- [Up, Right, Down, Left],
                           dir' /= flip dir,
                           corresponds (chart ! add idx dir') dir']
    in newdir
steer _ idx ' ' dir = error ("Went into spaces with direction " ++ show dir ++ " at " ++ show idx)

follow :: Chart -> Idx -> Dir -> ([Char], Int)
follow chart idx dir =
    if isAlpha (chart ! idx)
        then if chart ! add idx dir == ' '
                then ([chart ! idx], 1)
                else let (str, count) = follow chart (add idx dir) dir
                     in (chart ! idx : str, count + 1)
        else let newdir = steer chart idx (chart ! idx) dir
             in fmap succ $ follow chart (add idx newdir) newdir

findStart :: Chart -> Idx
findStart chart =
    let ((0, 0), (_, w')) = bounds chart
        [x] = [i | i <- [0..w'], chart ! (0, i) /= ' ']
    in (0, x)

main :: IO ()
main = do
    input <- (readFile "19.in")
    let w = length (head (lines input))
        h = length (lines input)
        chart = listArray ((0, 0), (h-1, w-1)) (filter (/= '\n') input)

    let start = findStart chart
        (str, count) = follow chart start Down
    putStrLn str
    print count